Collection syntax change because of Sonar
[aaf/authz.git] / misc / rosetta / src / main / java / org / onap / aaf / misc / rosetta / JaxSet.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.rosetta;
23
24 import java.lang.reflect.Method;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 import javax.xml.bind.annotation.XmlType;
30
31 /**
32  * For specific XML class, quickly find a Setter Method which will load the object
33  * 
34  * Object type of Setter must match String at this time.
35  * 
36  * @author Jonathan
37  *
38  * @param <T>
39  */
40 public class JaxSet<T> {
41         private static Map<Class<?>,JaxSet<?>> jsets = new HashMap<>();
42         private Map<String,Setter<T>> members;
43
44         private JaxSet(Class<?> cls) {
45                 members = new TreeMap<>();
46                 XmlType xmltype = cls.getAnnotation(XmlType.class);
47                 Class<?> paramType[] = new Class[] {String.class};
48                 for(String str : xmltype.propOrder()) {
49                         try {
50                                 String setName = "set" + Character.toUpperCase(str.charAt(0)) + str.subSequence(1, str.length());
51                                 Method meth = cls.getMethod(setName,paramType );
52                                 if(meth!=null) {
53                                         members.put(str, new Setter<T>(meth) {
54                                                 public void set(T o, Object t) throws ParseException {
55                                                         try {
56                                                                 this.meth.invoke(o, t);
57                                                         } catch (Exception e) {
58                                                                 throw new ParseException(e);
59                                                         }
60                                                 }
61                                         });
62                                 }
63                         } catch (Exception e) {
64                                 // oops
65                         }
66                 }
67         }
68         
69         public static abstract class Setter<O> {
70                 protected final Method meth;
71                 public Setter(Method meth) {
72                         this.meth = meth;
73                 }
74                 public abstract void set(O o, Object obj) throws ParseException;
75         }
76
77         public static <X> JaxSet<X> get(Class<?> cls) {
78                 synchronized(jsets) {
79                         @SuppressWarnings("unchecked")
80                         JaxSet<X> js = (JaxSet<X>)jsets.get(cls);
81                         if(js == null) {
82                                 jsets.put(cls, js = new JaxSet<>(cls));
83                         }
84                         return js;
85                 }
86         }
87
88         public Setter<T> get(String key) {
89                 return members.get(key);
90         }
91 }