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