1 /*
2 * Copyright 2016 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 package org.openehealth.ipf.commons.ihe.core;
17
18 import java.io.Serializable;
19
20 /**
21 *
22 */
23 public class SerializableEnumInteractionId<E extends Enum<E> & InteractionId> implements Serializable, InteractionId {
24 private static final long serialVersionUID = -2740691943593482233L;
25
26 private final String interaction;
27 private volatile transient E interactionId;
28
29 private SerializableEnumInteractionId(E interactionId) {
30 this.interactionId = interactionId;
31 this.interaction = interactionId.getClass().getName() + "$" + interactionId.name();
32 }
33
34 /**
35 * Create a serializable version of the given {@link InteractionId}
36 *
37 * @param interactionId interactionID
38 * @param <E> interaction ID type, which must be an enum
39 * @return serializable version of the given {@link InteractionId}
40 */
41 public static <E extends Enum<E> & InteractionId> SerializableEnumInteractionId create(E interactionId) {
42 return new SerializableEnumInteractionId(interactionId);
43 }
44
45 private static <E extends Enum<E> & InteractionId> E getInteractionId(String interaction) {
46 int lastIndex = interaction.lastIndexOf('$');
47 Class<E> enumType = null;
48 try {
49 enumType = (Class<E>) Class.forName(interaction.substring(0, lastIndex));
50 String enumValue = interaction.substring(lastIndex + 1);
51 return Enum.valueOf(enumType, enumValue);
52 } catch (ClassNotFoundException e) {
53 throw new IllegalArgumentException(e);
54 }
55 }
56
57 /**
58 * @return the (non-serializable) interactionId
59 */
60 public E getInteractionId() {
61 if (interactionId == null) {
62 interactionId = getInteractionId(interaction);
63 }
64 return interactionId;
65 }
66
67 /*
68 @Override
69 public String getName() {
70 return getInteractionId().getName();
71 }
72
73 @Override
74 public String getDescription() {
75 return getInteractionId().getDescription();
76 }
77
78 @Override
79 public boolean isQuery() {
80 return getInteractionId().isQuery();
81 }
82
83 @Override
84 public <T extends AuditDataset> AuditStrategy<T> getClientAuditStrategy() {
85 return getInteractionId().getClientAuditStrategy();
86 }
87
88 @Override
89 public <T extends AuditDataset> AuditStrategy<T> getServerAuditStrategy() {
90 return getInteractionId().getServerAuditStrategy();
91 }
92 */
93 }