]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/ResourceArray.java
Merge commit 'bf75fd9'
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / ResourceArray.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.common;\r
13 \r
14 import java.util.Arrays;\r
15 import java.util.Collection;\r
16 import java.util.Iterator;\r
17 \r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.common.utils.NameUtils;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.exception.ValidationException;\r
23 \r
24 /**\r
25  * @author Toni Kalajainen\r
26  */\r
27 public class ResourceArray implements Iterable<Resource> {\r
28 \r
29     public static final ResourceArray[] NONE  = new ResourceArray[0];\r
30 \r
31     public static final ResourceArray   EMPTY = new ResourceArray();\r
32 \r
33     public final Resource[]             resources;\r
34 \r
35     private int                         hash;\r
36 \r
37     private boolean noNulls(Resource[] rs) {\r
38         for(Resource r : rs) if(r == null) return false;\r
39         return true;\r
40     }\r
41     \r
42     private boolean noNulls(Collection<Resource> rs) {\r
43         for(Resource r : rs) if(r == null) return false;\r
44         return true;\r
45     }\r
46 \r
47     public ResourceArray(Resource... resources) {\r
48         assert(noNulls(resources));\r
49         this.resources = resources;\r
50         this.hash = Arrays.hashCode(resources);\r
51     }\r
52 \r
53     public ResourceArray(Collection<Resource> resources) {\r
54         assert(noNulls(resources));\r
55         this.resources = resources.toArray(new Resource[resources.size()]);\r
56         this.hash = Arrays.hashCode(this.resources);\r
57     }\r
58 \r
59     @Override\r
60     public int hashCode() {\r
61         return hash;\r
62     }\r
63 \r
64     @Override\r
65     public boolean equals(Object obj) {\r
66         if (this == obj)\r
67             return true;\r
68         if (!(obj instanceof ResourceArray))\r
69             return false;\r
70         ResourceArray other = (ResourceArray) obj;\r
71         return Arrays.deepEquals(this.resources, other.resources);\r
72     }\r
73 \r
74     @Override\r
75     public String toString() {\r
76         StringBuilder sb = new StringBuilder();\r
77         sb.append("[");\r
78         for (int i = 0; i < resources.length; i++) {\r
79             if (i > 0)\r
80                 sb.append(", ");\r
81             sb.append(resources[i].getResourceId());\r
82         }\r
83         sb.append("]");\r
84         return sb.toString();\r
85     }\r
86 \r
87     public String toString(ReadGraph g) throws DatabaseException {\r
88         StringBuilder sb = new StringBuilder();\r
89         sb.append("[");\r
90         for (int i = 0; i < resources.length; i++) {\r
91             if (i > 0)\r
92                 sb.append(", ");\r
93             sb.append(NameUtils.getSafeName(g, resources[i]));\r
94         }\r
95         sb.append("]");\r
96         return sb.toString();\r
97     }\r
98 \r
99     public static ResourceArray[] toSingleElementArrays(Resource[] array) {\r
100         ResourceArray[] result = new ResourceArray[array.length];\r
101         for (int i = 0; i < result.length; i++)\r
102             result[i] = new ResourceArray(array[i]);\r
103         return result;\r
104     }\r
105     \r
106     public Resource toSingleResource() throws ValidationException {\r
107         if(resources.length != 1) throw new ValidationException("Resource array did not contain a single resource (contained " + resources.length + ").");\r
108         return resources[0];\r
109     }\r
110 \r
111     public boolean isEmpty() {\r
112         return resources.length == 0;\r
113     }\r
114 \r
115     public int size() {\r
116         return resources.length;\r
117     }\r
118 \r
119     /**\r
120      * @param i the index of the resource to return\r
121      * @return ith resource of the array\r
122      * @since 1.6\r
123      */\r
124     public Resource get(int i) {\r
125         return resources[i];\r
126     }\r
127 \r
128     @Override\r
129     public Iterator<Resource> iterator() {\r
130         return Arrays.asList(resources).iterator();\r
131     }\r
132 \r
133     /**\r
134      * Returns a new ResourceArray instance that contains the resource of this\r
135      * ResourceArray with the specified resources appended at the end.\r
136      * \r
137      * @param append the resources to append at the end of the new array\r
138      * @return a new appended ResourceArray\r
139      */\r
140     public ResourceArray appended(Resource... append) {\r
141         if (append.length == 0)\r
142             return this;\r
143         Resource[] result = Arrays.copyOf(resources, resources.length + append.length);\r
144         System.arraycopy(append, 0, result, resources.length, append.length);\r
145         return new ResourceArray(result);\r
146     }\r
147 \r
148     /**\r
149      * Returns a new ResourceArray instance that contains the resource of this\r
150      * ResourceArray with the resources contained in the specified ResourceArray appended at the end.\r
151      * \r
152      * @param append the ResourceArray to append at the end of the new array\r
153      * @return a new appended ResourceArray\r
154      */\r
155     public ResourceArray appended(ResourceArray append) {\r
156         if (append.size() == 0)\r
157             return this;\r
158         Resource[] result = Arrays.copyOf(resources, resources.length + append.size());\r
159         System.arraycopy(append.resources, 0, result, resources.length, append.size());\r
160         return new ResourceArray(result);\r
161     }\r
162     \r
163     /**\r
164      * Returns a new ResourceArray instance that contains the resource of this\r
165      * ResourceArray with the specified resources prepended at the beginning.\r
166      * \r
167      * @param prepend the resources to prepend at the beginning of the new array\r
168      * @return a new prepended ResourceArray\r
169      */\r
170     public ResourceArray prepended(Resource... prepend) {\r
171         if (prepend.length == 0)\r
172             return this;\r
173         Resource[] result = Arrays.copyOf(prepend, prepend.length + resources.length);\r
174         System.arraycopy(resources, 0, result, prepend.length, resources.length);\r
175         return new ResourceArray(result);\r
176     }\r
177 \r
178     /**\r
179      * @param n\r
180      * @return\r
181      * @throws IllegalArgumentException\r
182      */\r
183     public ResourceArray removeFromBeginning(int n) throws IllegalArgumentException {\r
184         return slice(n, resources.length);\r
185     }\r
186 \r
187     /**\r
188      * @param n\r
189      * @return\r
190      * @throws IllegalArgumentException\r
191      */\r
192     public ResourceArray removeFromEnd(int n) throws IllegalArgumentException {\r
193         return slice(0, resources.length - n);\r
194     }\r
195 \r
196     public ResourceArray slice(int start, int end) {\r
197         return ((start == 0) && (end == resources.length)) ? this :\r
198             new ResourceArray(Arrays.copyOfRange(resources, start, end));\r
199     }\r
200 \r
201     public ResourceArray reversed() {\r
202         if (resources.length < 2)\r
203             return this;\r
204         Resource[] result = new Resource[resources.length];\r
205 //        for (int i = 0, j = resources.length - 1; i < resources.length / 2; ++i, --j) {\r
206 //            result[j] = resources[i];\r
207 //            result[i] = resources[j];\r
208 //        }\r
209         for (int i = 0; i < resources.length ; ++i) {\r
210                 result[i] = resources[resources.length - 1 - i];\r
211         }\r
212         return new ResourceArray(result);\r
213     }\r
214     \r
215     public Resource tail() {\r
216         return resources[resources.length - 1];\r
217     }\r
218 \r
219     public Resource head() {\r
220         return resources[0];\r
221     }\r
222     \r
223 }\r