1 /*
2 * *************************************************************************************************************************************************************
3 *
4 * TheseFoolishThings: Miscellaneous utilities
5 * http://tidalwave.it/projects/thesefoolishthings
6 *
7 * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8 *
9 * *************************************************************************************************************************************************************
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18 *
19 * *************************************************************************************************************************************************************
20 *
21 * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22 * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23 *
24 * *************************************************************************************************************************************************************
25 */
26 package it.tidalwave.role.ui;
27
28 import javax.annotation.Nonnull;
29 import java.util.Collection;
30 import java.util.List;
31 import java.util.Optional;
32 import it.tidalwave.util.NotFoundException;
33
34 /***************************************************************************************************************************************************************
35 *
36 * A role that provides {@link UserAction}s.
37 *
38 * @stereotype role
39 *
40 * @author Fabrizio Giudici
41 *
42 **************************************************************************************************************************************************************/
43 public interface UserActionProvider
44 {
45 public static final Class<UserActionProvider> _UserActionProvider_ = UserActionProvider.class;
46
47 /***********************************************************************************************************************************************************
48 * Returns a collection of {@link UserAction}s.
49 *
50 * @return a collection of actions
51 **********************************************************************************************************************************************************/
52 @Nonnull
53 public Collection<? extends UserAction> getActions();
54
55 /***********************************************************************************************************************************************************
56 * Returns the default action, if available.
57 *
58 *
59 * @return the default action
60 * @throws NotFoundException if there's no default action
61 * @deprecated Use {@link #getOptionalDefaultAction()}
62 **********************************************************************************************************************************************************/
63 @Nonnull @Deprecated
64 public UserAction getDefaultAction()
65 throws NotFoundException;
66
67 /***********************************************************************************************************************************************************
68 * Returns the default action, if available.
69 *
70 * @since 3.1-ALPHA-2
71 * @return the default action
72 **********************************************************************************************************************************************************/
73 @Nonnull
74 public default Optional<UserAction> getOptionalDefaultAction()
75 {
76 try
77 {
78 return Optional.of(getDefaultAction());
79 }
80 catch (NotFoundException e)
81 {
82 return Optional.empty();
83 }
84 }
85
86 /***********************************************************************************************************************************************************
87 * Factory method which creates an instance out of an array of {@link UserAction}s. The first one is considered the
88 * default action.
89 *
90 * @since 3.1-ALPHA-2
91 * @param actions the actions
92 * @return the {@code UserActionProvider}
93 **********************************************************************************************************************************************************/
94 @Nonnull
95 public static UserActionProvider of (@Nonnull final UserAction ... actions)
96 {
97 return new UserActionProvider()
98 {
99 @Override @Nonnull
100 public Collection<? extends UserAction> getActions()
101 {
102 return List.of(actions);
103 }
104
105 @Override @Nonnull
106 public UserAction getDefaultAction()
107 throws NotFoundException
108 {
109 if (actions.length == 0)
110 {
111 throw new NotFoundException();
112 }
113
114 return actions[0];
115 }
116 };
117 }
118 }