Frames | No Frames |
1: /* SimpleList.java -- simple way to make tuples. 2: Copyright (C) 2004, 2006 Free Software Foundation, Inc. 3: 4: This file is a part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2 of the License, or (at 9: your option) any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; if not, write to the Free Software 18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19: USA 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.java.security.util; 40: 41: import java.util.AbstractList; 42: import java.util.Collection; 43: import java.util.Iterator; 44: 45: /** 46: * A simple way to create immutable n-tuples. This class can be created with up 47: * to four elements specified via one of the constructors, or with a collection 48: * of arbitrary size. 49: */ 50: public final class SimpleList 51: extends AbstractList 52: { 53: private final Object[] elements; 54: 55: /** 56: * Create a singleton list. 57: * 58: * @param element The first element. 59: */ 60: public SimpleList(final Object element) 61: { 62: elements = new Object[1]; 63: elements[0] = element; 64: } 65: 66: /** 67: * Create an ordered pair (2-tuple). 68: * 69: * @param e1 The first element. 70: * @param e2 The second element. 71: */ 72: public SimpleList(final Object e1, final Object e2) 73: { 74: elements = new Object[2]; 75: elements[0] = e1; 76: elements[1] = e2; 77: } 78: 79: /** 80: * Create a 3-tuple. 81: * 82: * @param e1 The first element. 83: * @param e2 The second element. 84: * @param e3 The third element. 85: */ 86: public SimpleList(final Object e1, final Object e2, final Object e3) 87: { 88: elements = new Object[3]; 89: elements[0] = e1; 90: elements[1] = e2; 91: elements[2] = e3; 92: } 93: 94: /** 95: * Create a 4-tuple. 96: * 97: * @param e1 The first element. 98: * @param e2 The second element. 99: * @param e3 The third element. 100: * @param e4 The fourth element. 101: */ 102: public SimpleList(final Object e1, final Object e2, final Object e3, 103: final Object e4) 104: { 105: elements = new Object[4]; 106: elements[0] = e1; 107: elements[1] = e2; 108: elements[2] = e3; 109: elements[3] = e4; 110: } 111: 112: /** 113: * Create the empty list. 114: */ 115: public SimpleList() 116: { 117: elements = null; 118: } 119: 120: /** 121: * Create an n-tuple of arbitrary size. Even if the supplied collection has no 122: * natural order, the created n-tuple will have the order that the elements 123: * are returned by the collection's iterator. 124: * 125: * @param c The collection. 126: */ 127: public SimpleList(Collection c) 128: { 129: elements = new Object[c.size()]; 130: int i = 0; 131: for (Iterator it = c.iterator(); it.hasNext() && i < elements.length;) 132: elements[i++] = it.next(); 133: } 134: 135: public int size() 136: { 137: if (elements == null) 138: return 0; 139: return elements.length; 140: } 141: 142: public Object get(int index) 143: { 144: if (elements == null) 145: throw new IndexOutOfBoundsException("list is empty"); 146: if (index < 0 || index >= elements.length) 147: throw new IndexOutOfBoundsException("index=" + index + ", size=" + size()); 148: return elements[index]; 149: } 150: 151: public String toString() 152: { 153: return SimpleList.class.getName() + "(" + size() + ") " + super.toString(); 154: } 155: }