minor simplification in INeutronRequest
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / INeutronRequest.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.neutron.northbound.api;
10
11 import java.lang.reflect.Field;
12 import java.util.List;
13 import org.opendaylight.neutron.spi.INeutronObject;
14
15 public interface INeutronRequest<T extends INeutronObject<T>> {
16
17     default T getSingleton() {
18         // return this.singleton
19         Class<?> cls = getClass();
20         try {
21             Field field = cls.getDeclaredField("singleton");
22             @SuppressWarnings("unchecked")
23             T value = (T) field.get(this);
24             return value;
25         } catch (IllegalAccessException | NoSuchFieldException e) {
26             throw new IllegalArgumentException(e);
27         }
28     }
29
30     default boolean isSingleton() {
31         return getSingleton() != null;
32     }
33
34     default List<T> getBulk() {
35         // return this.bulkRequest
36         Class<?> cls = getClass();
37         try {
38             Field field = cls.getDeclaredField("bulkRequest");
39             @SuppressWarnings("unchecked")
40             List<T> value = (List<T>) field.get(this);
41             return value;
42         } catch (IllegalAccessException | NoSuchFieldException e) {
43             throw new IllegalArgumentException(e);
44         }
45     }
46 }