OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 174
Line 200... Line 200...
200
     * @param <S> type of SQLElement
200
     * @param <S> type of SQLElement
201
     * @param clazz the class.
201
     * @param clazz the class.
202
     * @return the corresponding SQLElement, or <code>null</code> if none can be found.
202
     * @return the corresponding SQLElement, or <code>null</code> if none can be found.
203
     * @throws IllegalArgumentException if there's more than one match.
203
     * @throws IllegalArgumentException if there's more than one match.
204
     */
204
     */
205
    public synchronized final <S extends SQLElement> S getElement(Class<S> clazz) {
205
    public final <S extends SQLElement> S getElement(Class<S> clazz) {
206
        return clazz.cast(this.getElement(getSoleTable(this.byClass, clazz)));
206
        return this.getElementOfClass(clazz, false);
-
 
207
    }
-
 
208
 
-
 
209
    public final <S extends SQLElement> S getElementOfClass(Class<S> clazz, final boolean allowSubclass) {
-
 
210
        final List<S> res = getElementsOfClass(clazz, allowSubclass);
-
 
211
        if (res.size() > 1)
-
 
212
            throw new IllegalArgumentException(clazz + " is not unique: " + res);
-
 
213
        return res.isEmpty() ? null : res.get(0);
-
 
214
    }
-
 
215
 
-
 
216
    /**
-
 
217
     * Search for SQLElement whose class is <code>clazz</code>.
-
 
218
     * 
-
 
219
     * @param clazz the class.
-
 
220
     * @param allowSubclass <code>true</code> to include {@link Class#isInstance(Object)
-
 
221
     *        subclasses}, <code>false</code> to only return exact match.
-
 
222
     * @return the corresponding elements.
-
 
223
     */
-
 
224
    public final <S extends SQLElement> List<S> getElementsOfClass(final Class<S> clazz, final boolean allowSubclass) {
-
 
225
        final List<S> res;
-
 
226
        synchronized (this) {
-
 
227
            if (allowSubclass) {
-
 
228
                res = new ArrayList<>();
-
 
229
                for (final SQLElement elem : this.elements.values()) {
-
 
230
                    if (clazz.isInstance(elem))
-
 
231
                        res.add(clazz.cast(elem));
-
 
232
                }
-
 
233
            } else {
-
 
234
                final Set<SQLTable> tables = this.byClass.get(clazz);
-
 
235
                if (tables != null) {
-
 
236
                    res = new ArrayList<>(tables.size());
-
 
237
                    for (final SQLTable t : tables) {
-
 
238
                        res.add(clazz.cast(this.getElement(t)));
-
 
239
                    }
-
 
240
                } else {
-
 
241
                    res = Collections.emptyList();
-
 
242
                }
-
 
243
            }
-
 
244
        }
-
 
245
        if (res.isEmpty()) {
-
 
246
            return Collections.emptyList();
-
 
247
        } else if (res.size() == 1) {
-
 
248
            return Collections.singletonList(res.get(0));
-
 
249
        } else {
-
 
250
            return Collections.unmodifiableList(res);
-
 
251
        }
207
    }
252
    }
208
 
253
 
209
    public synchronized final SQLElement getElementForCode(String code) {
254
    public synchronized final SQLElement getElementForCode(String code) {
210
        return this.getElement(getSoleTable(this.byCode, code));
255
        return this.getElement(getSoleTable(this.byCode, code));
211
    }
256
    }