| Home >> All >> org >> bluej >> [ core Javadoc ] |
Source code: org/bluej/core/DataElement.java
1 package org.bluej.core; 2 3 public class DataElement { 4 public static final int NULL = 0x00; 5 public static final int U_INT_1 = 0x08; 6 public static final int U_INT_2 = 0x09; 7 public static final int U_INT_4 = 0x0a; 8 public static final int U_INT_8 = 0x0b; 9 public static final int U_INT_16 = 0x0c; 10 public static final int INT_1 = 0x10; 11 public static final int INT_2 = 0x11; 12 public static final int INT_4 = 0x12; 13 public static final int INT_8 = 0x13; 14 public static final int INT_16 = 0x14; 15 public static final int URL = 0x40; 16 public static final int UUID = 0x18; 17 public static final int BOOL = 0x28; 18 public static final int STRING = 0x20; 19 public static final int DATSEQ = 0x30; 20 public static final int DATALT = 0x38; 21 22 int type; 23 Object val; 24 25 class List { 26 DataElement elt; 27 List next; 28 List(DataElement elem) { 29 elt = elem; 30 } 31 } 32 public class ListScanner implements java.util.Enumeration { 33 List scan; 34 ListScanner(List val) { 35 scan = (List)val; 36 } 37 public boolean hasMoreElements() { 38 return !(scan == null); 39 } 40 public Object nextElement() { 41 Object ret = scan.elt; 42 scan = scan.next; 43 return ret; 44 } 45 } 46 47 public DataElement(int valueType) { 48 type = valueType; 49 switch (valueType) { 50 case NULL: case DATALT: case DATSEQ: 51 val = null; 52 break; 53 default: 54 throw new IllegalArgumentException( 55 "only NULL, DATALT or DATSEQ allowed"); 56 } 57 } 58 public DataElement(boolean bool) { 59 type = BOOL; 60 val = new Boolean(bool); 61 } 62 public DataElement(int valueType, long value) { 63 type = valueType; 64 switch (valueType) { 65 case U_INT_1: case U_INT_2: case U_INT_4: 66 case INT_1: case INT_2: case INT_4: case INT_8: 67 val = new Long(value); 68 break; 69 default: 70 throw new IllegalArgumentException( 71 "only integers (except U_INT_8) allowed"); 72 } 73 } 74 public DataElement(int valueType, Object value) { 75 boolean ok = false; 76 type = valueType; 77 val = value; 78 switch (valueType) { 79 case URL: case STRING: 80 if (val instanceof String) 81 ok = true; 82 break; 83 case UUID: 84 if (val instanceof UUID) 85 ok = true; 86 break; 87 case INT_16: case U_INT_8: case U_INT_16: 88 if (val instanceof byte[]) { 89 byte[] vec = (byte[])val; 90 if (vec.length == (valueType == U_INT_8 ? 8 : 16)) 91 ok = true; 92 } 93 break; 94 } 95 if (!ok) 96 throw new IllegalArgumentException( "incorrect argument"); 97 } 98 99 public void addElement(DataElement elem) { 100 if (type != DATSEQ && type != DATALT) 101 throw new ClassCastException("incorrect argument"); 102 if (elem == null) 103 throw new NullPointerException("null in addElement"); 104 List elt = new List(elem); 105 if (val == null) { 106 val = elt; 107 } 108 else { 109 List last = (List)val; 110 List next = last.next; 111 while (next != null) { 112 last = next; 113 next = last.next; 114 } 115 last.next = elt; 116 } 117 } 118 119 public int getSize() { 120 if (type != DATSEQ && type != DATALT) 121 throw new ClassCastException("incorrect argument"); 122 int size = 0; 123 List scan = (List)val; 124 while (scan != null) { 125 size++; 126 scan = scan.next; 127 } 128 return size; 129 } 130 131 public void insertElementAt(DataElement elem, int index) { 132 if (type != DATSEQ && type != DATALT) 133 throw new ClassCastException("incorrect argument"); 134 if (elem == null) 135 throw new NullPointerException("null in addElement"); 136 if (index < 0 || index > getSize()) 137 throw new IndexOutOfBoundsException("bad index " + index); 138 List elt = new List(elem); 139 if (index == 0) { 140 elt.next = (List)val; 141 val = elt; 142 } 143 else { 144 List last = null; 145 List scan = (List)val; 146 for (int i = 0; i < index-1; i++) { 147 last = scan; 148 scan = scan.next; 149 } 150 if (last == null) { 151 elt.next = (List)val; 152 val = elt; 153 } 154 else { 155 elt.next = last.next; 156 last.next = elt; 157 } 158 } 159 } 160 public boolean removeElement(DataElement elem) { 161 if (type != DATSEQ && type != DATALT) 162 throw new ClassCastException("incorrect argument"); 163 if (elem == null) 164 throw new NullPointerException("null in addElement"); 165 List last = null; 166 List scan = (List)val; 167 while (scan != null) { 168 if (scan.elt == elem) { 169 if (last == null) 170 val = scan; 171 else 172 last.next = scan.next; 173 return true; 174 } 175 last = scan; 176 scan = scan.next; 177 } 178 return false; 179 } 180 181 public int getDataType() { 182 return type; 183 } 184 185 public long getLong() { 186 Long l = (Long)val; 187 return l.longValue(); 188 } 189 public boolean getBoolean() { 190 Boolean b = (Boolean)val; 191 return b.booleanValue(); 192 } 193 public Object getValue() { 194 Object ret; 195 switch (type) { 196 case DATALT: case DATSEQ: 197 ret = new ListScanner((List)val); 198 break; 199 case INT_16: case U_INT_8: case U_INT_16: 200 case URL: case UUID: case STRING: 201 ret = val; 202 break; 203 default: 204 throw new ClassCastException("cannot be an integer"); 205 } 206 return ret; 207 } 208 }