%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollectionUtil |
|
|
1 | /* |
|
2 | * Copyright 2004-2005 The Apache Software Foundation or its licensors, |
|
3 | * as applicable. |
|
4 | * |
|
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 | * you may not use this file except in compliance with the License. |
|
7 | * You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | package org.apache.portals.graffito.jcr.persistence.collectionconverter; |
|
18 | ||
19 | ||
20 | import java.util.ArrayList; |
|
21 | import java.util.Collection; |
|
22 | import java.util.List; |
|
23 | import java.util.Vector; |
|
24 | ||
25 | import org.apache.portals.graffito.jcr.exception.JcrMappingException; |
|
26 | import org.apache.portals.graffito.jcr.persistence.collectionconverter.impl.ManageableArrayList; |
|
27 | import org.apache.portals.graffito.jcr.persistence.collectionconverter.impl.ManageableVector; |
|
28 | ||
29 | /** |
|
30 | * Utility class used to instantiate {@link ManageableCollection} |
|
31 | * |
|
32 | * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a> |
|
33 | * |
|
34 | */ |
|
35 | 0 | public class ManageableCollectionUtil { |
36 | ||
37 | /** |
|
38 | * Instantiate a new {@link ManageableCollection} |
|
39 | * @param manageableCollectionClassName The manageable collection class name |
|
40 | * @return an emtpy created {@link ManageableCollection} |
|
41 | */ |
|
42 | public static ManageableCollection getManageableCollection(String manageableCollectionClassName) { |
|
43 | try { |
|
44 | 0 | Class collectionClass = Class.forName(manageableCollectionClassName); |
45 | ||
46 | 0 | return (ManageableCollection) collectionClass.newInstance(); |
47 | } |
|
48 | 0 | catch (Exception e) { |
49 | 0 | throw new JcrMappingException("Cannot create manageable collection : " |
50 | + manageableCollectionClassName, |
|
51 | e); |
|
52 | } |
|
53 | } |
|
54 | ||
55 | /** |
|
56 | * Instantiate a new {@link ManageableCollection} |
|
57 | * @param collectionClass the collection class name |
|
58 | * @return an emtpy created {@link ManageableCollection} |
|
59 | */ |
|
60 | ||
61 | public static ManageableCollection getManageableCollection(Class collectionClass) { |
|
62 | try { |
|
63 | ||
64 | 0 | if (collectionClass.equals(ArrayList.class)) { |
65 | 0 | return new ManageableArrayList(); |
66 | } |
|
67 | ||
68 | 0 | if (collectionClass.equals(Vector.class)) { |
69 | 0 | return new ManageableVector(); |
70 | } |
|
71 | ||
72 | 0 | if (collectionClass.equals(Collection.class) || collectionClass.equals(List.class)) { |
73 | 0 | return new ManageableArrayList(); |
74 | } |
|
75 | ||
76 | 0 | Object collection = collectionClass.newInstance(); |
77 | 0 | if (!(collection instanceof ManageableCollection)) { |
78 | 0 | throw new JcrMappingException("Unsupported collection type :" |
79 | + collectionClass.getName()); |
|
80 | } |
|
81 | else { |
|
82 | 0 | return (ManageableCollection) collection; |
83 | } |
|
84 | } |
|
85 | 0 | catch (Exception e) { |
86 | 0 | throw new JcrMappingException("Cannot create manageable collection", e); |
87 | } |
|
88 | } |
|
89 | ||
90 | /** |
|
91 | * Convert a java Collection object into a {@link ManageableCollection}. |
|
92 | * Until now, only the following class are supported : |
|
93 | * Collection, List, ArrayList, Vector |
|
94 | * |
|
95 | * If you need a Map, you have to write your own {@link ManageableCollection}. |
|
96 | * @param object the java collection or Map |
|
97 | * @return The converted {@link ManageableCollection} |
|
98 | * |
|
99 | */ |
|
100 | public static ManageableCollection getManageableCollection(Object object) { |
|
101 | try { |
|
102 | 0 | if (object == null) { |
103 | 0 | return null; |
104 | } |
|
105 | ||
106 | 0 | if (object instanceof ManageableCollection) { |
107 | 0 | return (ManageableCollection) object; |
108 | ||
109 | } |
|
110 | 0 | if (object.getClass().equals(ArrayList.class)) { |
111 | 0 | ManageableArrayList manageableArrayList = new ManageableArrayList(); |
112 | 0 | manageableArrayList.addAll((Collection) object); |
113 | ||
114 | 0 | return manageableArrayList; |
115 | } |
|
116 | ||
117 | 0 | if (object.getClass().equals(Vector.class)) { |
118 | 0 | ManageableVector manageableVector = new ManageableVector(); |
119 | 0 | manageableVector.addAll((Collection) object); |
120 | ||
121 | 0 | return manageableVector; |
122 | } |
|
123 | ||
124 | 0 | if (object.getClass().equals(Collection.class) |
125 | || object.getClass().equals(List.class)) { |
|
126 | 0 | ManageableArrayList manageableArrayList = new ManageableArrayList(); |
127 | 0 | manageableArrayList.addAll((Collection) object); |
128 | ||
129 | 0 | return manageableArrayList; |
130 | } |
|
131 | } |
|
132 | 0 | catch (Exception e) { |
133 | 0 | throw new JcrMappingException("Impossible to create the manageable collection", e); |
134 | 0 | } |
135 | ||
136 | 0 | throw new JcrMappingException("Unsupported collection type :" + object.getClass().getName()); |
137 | } |
|
138 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |