KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > map > EnumList


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.jpa.map;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25
26 /**
27  * A list that converts added objects to the specified enum type.
28  *
29  * @author Andrus Adamchik
30  */

31 // TODO: andrus, 4/20/2006 - remove this class, replacing it with parameterized
32
// collections once CAY-520 gets implemented in Cayenne > 1.2
33
class EnumList extends ArrayList JavaDoc {
34
35     private Class JavaDoc enumClass;
36
37     EnumList(Class JavaDoc enumClass, int capacity) {
38         super(capacity);
39         this.enumClass = enumClass;
40     }
41
42     private Object JavaDoc convertValue(Object JavaDoc value) {
43         if (value instanceof String JavaDoc) {
44             value = Enum.valueOf(enumClass, value.toString());
45         }
46
47         return value;
48     }
49     
50     private Collection JavaDoc convertValues(Collection JavaDoc values) {
51         if(values != null && !values.isEmpty()) {
52             Collection JavaDoc converted = new ArrayList JavaDoc(values.size());
53             for(Object JavaDoc value : values) {
54                 converted.add(convertValue(value));
55             }
56             
57             return converted;
58         }
59         else {
60             return values;
61         }
62     }
63
64     @Override JavaDoc
65     public void add(int index, Object JavaDoc element) {
66         super.add(index, convertValue(element));
67     }
68
69     @Override JavaDoc
70     public boolean add(Object JavaDoc o) {
71         return super.add(convertValue(o));
72     }
73
74     @Override JavaDoc
75     public boolean addAll(Collection JavaDoc c) {
76         return super.addAll(convertValues(c));
77     }
78
79     @Override JavaDoc
80     public boolean addAll(int index, Collection JavaDoc c) {
81         return super.addAll(index, convertValues(c));
82     }
83 }
84
Popular Tags