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