1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.Map.Entry;
41
42
43
44
45
46
47 public class ReferenceList extends ArrayList<ReferenceItem>
48 {
49
50
51
52 private static final long serialVersionUID = 5456351278712947650L;
53
54
55
56
57 public ReferenceList( )
58 {
59 super( );
60 }
61
62
63
64
65
66 public ReferenceList( int nInitialCapacity )
67 {
68 super( nInitialCapacity );
69 }
70
71
72
73
74
75
76
77 public void addItem( String strCode, String strName )
78 {
79 ReferenceItem item = new ReferenceItem( );
80 item.setCode( strCode );
81 item.setName( strName );
82 add( item );
83 }
84
85
86
87
88
89
90
91
92
93 public void addItem( int nCode, String strName )
94 {
95 ReferenceItem item = new ReferenceItem( );
96 String strCode = String.valueOf( nCode );
97 item.setCode( strCode );
98 item.setName( strName );
99 add( item );
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public static ReferenceList convert( Collection collection, String strCodeAttribute, String strNameAttribute,
115 boolean bNumericCode )
116 {
117 ReferenceList list = new ReferenceList( );
118 String strCodeGetter = "get" + Character.toUpperCase( strCodeAttribute.charAt( 0 ) ) +
119 strCodeAttribute.substring( 1 );
120 String strNameGetter = "get" + Character.toUpperCase( strNameAttribute.charAt( 0 ) ) +
121 strNameAttribute.substring( 1 );
122 String strCode;
123 String strName;
124
125 try
126 {
127 for ( Object o : collection )
128 {
129
130 if ( bNumericCode )
131 {
132 Integer nCode = (Integer) o.getClass( ).getMethod( strCodeGetter, (Class[]) null )
133 .invoke( o, (Object[]) null );
134 strCode = nCode.toString( );
135 }
136 else
137 {
138 strCode = (String) o.getClass( ).getMethod( strCodeGetter, (Class[]) null )
139 .invoke( o, (Object[]) null );
140 }
141
142 strName = (String) o.getClass( ).getMethod( strNameGetter, (Class[]) null ).invoke( o, (Object[]) null );
143 list.addItem( strCode, strName );
144 }
145 }
146 catch ( Exception ex )
147 {
148 return null;
149 }
150
151 return list;
152 }
153
154
155
156
157
158
159 public static ReferenceList convert( Map<String, String> map )
160 {
161 if ( map != null )
162 {
163 ReferenceList list = new ReferenceList( );
164
165 for ( Entry<String, String> param : map.entrySet( ) )
166 {
167 list.addItem( param.getKey( ), param.getValue( ) );
168 }
169
170 return list;
171 }
172
173 return null;
174 }
175
176
177
178
179
180 public Map<String, String> toMap( )
181 {
182 Map<String, String> map = new HashMap<String, String>( );
183
184 if ( !this.isEmpty( ) )
185 {
186 for ( ReferenceItem item : this )
187 {
188 map.put( item.getCode( ), item.getName( ) );
189 }
190 }
191
192 return map;
193 }
194
195
196
197
198
199 public void checkItems( String[] valuesToCheck )
200 {
201 for ( int i = 0; i < valuesToCheck.length; i++ )
202 {
203 for ( ReferenceItem item : this )
204 {
205 if ( item.getCode( ).equals( valuesToCheck[i] ) )
206 {
207 item.setChecked( true );
208 }
209 }
210 }
211 }
212 }