1 /*
2 * Copyright 2017 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.openehealth.ipf.commons.audit.types;
18
19 import java.util.stream.Stream;
20
21 /**
22 * This interface is intended to be implemented by enum classes in order to provide a
23 * set of code values. Complex codes (of type @link {@link CodedValueType}) should be
24 * inherited from {@link EnumeratedCodedValue}.
25 *
26 * @see EnumeratedCodedValue
27 *
28 * @author Christian Ohr
29 * @since 3.5
30 */
31 public interface EnumeratedValueSet<T> {
32
33 T getValue();
34
35 default boolean matches(Object code) {
36 return getValue().equals(code);
37 }
38
39 static <T, E extends Enum<E> & EnumeratedValueSet<T>> E enumForCode(Class<E> clazz, Object code) {
40 return Stream.of(clazz.getEnumConstants())
41 .filter(e -> e.matches(code))
42 .findFirst()
43 .orElseThrow(() -> new IllegalArgumentException(code.toString() + " is not a code for " + clazz.getSimpleName()));
44 }
45
46
47 }