Source for javax.print.attribute.SetOfIntegerSyntax

   1: /* SetOfIntegerSyntax.java -- 
   2:    Copyright (C) 2003, 2004  Free Software Foundation, Inc.
   3: 
   4: This file is 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, or (at your option)
   9: 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; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 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: package javax.print.attribute;
  39: 
  40: import java.io.Serializable;
  41: import java.util.Vector;
  42: 
  43: /**
  44:  * @author Michael Koch
  45:  */
  46: public abstract class SetOfIntegerSyntax
  47:   implements Cloneable, Serializable
  48: {
  49:   private static final long serialVersionUID = 3666874174847632203L;
  50: 
  51:   private int[][] members;
  52: 
  53:   private static int[][] normalize(Vector vecMembers)
  54:   {
  55:     // XXX: Perhaps we should merge ranges that overlap.
  56:     
  57:     int current = 0;
  58:     int[][] members = new int[vecMembers.size()][];
  59: 
  60:     while (vecMembers.size() > 0)
  61:       {
  62:         // Search the lowest range.
  63:         int[] range = (int[]) vecMembers.elementAt(0);
  64: 
  65:         for (int index = 1; index < vecMembers.size(); index++)
  66:           {
  67:             int[] tmp = (int[]) vecMembers.elementAt(index);
  68: 
  69:             if (range[0] > tmp[0]
  70:                 || (range[0] == tmp[0]
  71:                     && range[0] > tmp[0]))
  72:               range = tmp;
  73:           }
  74: 
  75:         members[current] = range;
  76:         current++;
  77:       }
  78:     
  79:     return members;
  80:   }
  81:   
  82:   /**
  83:    * Creates a <code>SetOfIntegerSyntax</code> object.
  84:    *
  85:    * @param member the member value
  86:    *
  87:    * @exception IllegalArgumentException if member is < 0
  88:    */
  89:   protected SetOfIntegerSyntax(int member)
  90:   {
  91:     if (member < 0)
  92:       throw new IllegalArgumentException("member may not be less than 0");
  93: 
  94:     this.members = new int[][]{{member, member}};
  95:   }
  96: 
  97:   /**
  98:    * Creates a <code>SetOfIntegerSyntax</code> object.
  99:    *
 100:    * @param members the members to use in this set
 101:    *
 102:    * @exception IllegalArgumentException if any element is invalid
 103:    * @exception NullPointerException if any element of members is null
 104:    */
 105:   protected SetOfIntegerSyntax(int[][] members)
 106:   {
 107:     Vector vecMembers = new Vector();
 108:     
 109:     if (members != null)
 110:       {
 111:         for (int index = 0; index < members.length; index++)
 112:           {
 113:             int lower;
 114:             int upper;
 115: 
 116:             if (members[index].length == 1)
 117:               {
 118:                 lower = members[index][0];
 119:                 upper = members[index][0];
 120:               }
 121:             else if (members[index].length == 2)
 122:               {
 123:                 lower = members[index][0];
 124:                 upper = members[index][1];
 125:               }
 126:             else
 127:               throw new IllegalArgumentException("invalid member element");
 128: 
 129:             if (lower <= upper && lower < 0)
 130:               throw new IllegalArgumentException("invalid member element");
 131: 
 132:             if (lower <= upper)
 133:               {
 134:                 int[] range = new int[2];
 135:                 range[0] = lower;
 136:                 range[1] = upper;
 137:                 vecMembers.add(range);
 138:               }
 139:           }
 140:       }
 141:     
 142:     this.members = normalize(vecMembers);
 143:   }
 144: 
 145:   /**
 146:    * Creates a <code>SetOfIntegerSyntax</code> object.
 147:    *
 148:    * @param lowerBound the lower bound value
 149:    * @param upperBound the upper bound value
 150:    *
 151:    * @exception IllegalArgumentException if lowerBound &lt;= upperbound
 152:    * and lowerBound &lt; 0
 153:    */
 154:   protected SetOfIntegerSyntax(int lowerBound, int upperBound)
 155:   {
 156:     if (lowerBound <= upperBound
 157:         && lowerBound < 0)
 158:       throw new IllegalArgumentException();
 159: 
 160:     members = (lowerBound <= upperBound ? new int[][]{{lowerBound, upperBound}}
 161:                                         : new int[0][]);
 162:   }
 163: 
 164:   /**
 165:    * Checks if this set contains value.
 166:    *
 167:    * @param value the value to test for
 168:    *
 169:    * @return true if this set contains value, false otherwise
 170:    */
 171:   public boolean contains(int value)
 172:   {
 173:     // This only works on a normalized member array.
 174:     for (int index = 0; index < members.length; index++)
 175:       {
 176:         if (value < members[index][0])
 177:           return false;
 178:         else if (value < members[index][1])
 179:           return true;
 180:       }
 181: 
 182:     return false;
 183:   }
 184: 
 185:   /**
 186:    * Checks if this set contains value.
 187:    *
 188:    * @param value the value to test for
 189:    *
 190:    * @return true if this set contains value, false otherwise
 191:    */
 192:   public boolean contains(IntegerSyntax value)
 193:   {
 194:     return contains(value.getValue());
 195:   }
 196: 
 197:   /**
 198:    * Tests of obj is equal to this object.
 199:    *
 200:    * @param obj the object to test
 201:    *
 202:    * @return true if both objects are equal, false otherwise.
 203:    */
 204:   public boolean equals(Object obj)
 205:   {
 206:     if (! (obj instanceof SetOfIntegerSyntax))
 207:       return false;
 208: 
 209:     throw new Error("not implemented");
 210:   }
 211: 
 212:   /**
 213:    * Returns an array describing the members included in this set.
 214:    *
 215:    * @return the array with the members
 216:    */
 217:   public int[][] getMembers()
 218:   {
 219:     throw new Error("not implemented");
 220:   }
 221: 
 222:   /**
 223:    * Returns the hashcode for this object.
 224:    *
 225:    * @return the hashcode
 226:    */
 227:   public int hashCode()
 228:   {
 229:     throw new Error("not implemented");
 230:   }
 231: 
 232:   /**
 233:    * Returns the smallest value that is greater then x.
 234:    *
 235:    * @param x an integer value
 236:    *
 237:    * @return the next value
 238:    */
 239:   public int next(int x)
 240:   {
 241:     throw new Error("not implemented");
 242:   }
 243: 
 244:   /**
 245:    * Returns the string representation for this object.
 246:    *
 247:    * @return the string representation
 248:    */
 249:   public String toString()
 250:   {
 251:     throw new Error("not implemented");
 252:   }
 253: }