4248b1dbcaefdbcd11bb10b3081c4898cfaa1bbc
[utils] / system / general / src / main / java / org / wamblee / system / core / ReadOnlyNamedInterfaces.java
1 /*
2  * Copyright 2008 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.system.core;
17
18 import java.lang.reflect.Array;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
23
24 /**
25  * A set of interfaces that cannot be changed with useful added functionality. 
26  * 
27  * @author Erik Brakkee
28  *
29  * @param <T>
30  */
31 public class ReadOnlyNamedInterfaces<T extends NamedInterface> implements Iterable<T> {
32
33     private Class<T> _type; 
34     private List<T> _interfaces; 
35
36     public ReadOnlyNamedInterfaces(Class<T> aType, List<T> aInterfaces) {
37         _type = aType; 
38         _interfaces = Collections.unmodifiableList(aInterfaces); 
39     }
40     
41     /**
42      * @return Number of interfaces. 
43      */
44     public int size() { 
45         return _interfaces.size();
46     }
47  
48     /**
49      * Gets the interface at a given index. 
50      * @param aIndex Index of the interface to return. 
51      * @return Interface.
52      */
53     public T get(int aIndex) { 
54         return _interfaces.get(aIndex);
55     }
56     
57     /**
58      * Gets the interface with a specific name. 
59      * @param aName Interface name. 
60      * @return Interface. 
61      */
62     public T get(String aName) { 
63         for (T intf: _interfaces) { 
64             if ( intf.getName().equals(aName)) { 
65                 return intf; 
66             }
67         }
68         return null; 
69     }
70     
71     @Override
72     public Iterator<T> iterator() {
73         return _interfaces.iterator();
74     }
75     
76     /**
77      * Converts the interfaces to an array. 
78      * @return List of interfaces in an array. 
79      */
80     public T[] toArray() { 
81         T[] storage = (T[])Array.newInstance(_type, _interfaces.size());
82         return _interfaces.toArray(storage);
83     }
84 }