src/share/classes/java/util/BitSet.java

Print this page


   1 /*
   2  * Copyright (c) 1995, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1065         if (wordsInUse != set.wordsInUse)
1066             return false;
1067 
1068         // Check words in use by both BitSets
1069         for (int i = 0; i < wordsInUse; i++)
1070             if (words[i] != set.words[i])
1071                 return false;
1072 
1073         return true;
1074     }
1075 
1076     /**
1077      * Cloning this {@code BitSet} produces a new {@code BitSet}
1078      * that is equal to it.
1079      * The clone of the bit set is another bit set that has exactly the
1080      * same bits set to {@code true} as this bit set.
1081      *
1082      * @return a clone of this bit set
1083      * @see    #size()
1084      */
1085     public Object clone() {

1086         if (! sizeIsSticky)
1087             trimToSize();
1088 
1089         try {

1090             BitSet result = (BitSet) super.clone();
1091             result.words = words.clone();
1092             result.checkInvariants();
1093             return result;
1094         } catch (CloneNotSupportedException e) {
1095             throw new InternalError(e);
1096         }
1097     }
1098 
1099     /**
1100      * Attempts to reduce internal storage used for the bits in this bit set.
1101      * Calling this method may, but is not required to, affect the value
1102      * returned by a subsequent call to the {@link #size()} method.
1103      */
1104     private void trimToSize() {
1105         if (wordsInUse != words.length) {
1106             words = Arrays.copyOf(words, wordsInUse);
1107             checkInvariants();
1108         }
1109     }


   1 /*
   2  * Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1065         if (wordsInUse != set.wordsInUse)
1066             return false;
1067 
1068         // Check words in use by both BitSets
1069         for (int i = 0; i < wordsInUse; i++)
1070             if (words[i] != set.words[i])
1071                 return false;
1072 
1073         return true;
1074     }
1075 
1076     /**
1077      * Cloning this {@code BitSet} produces a new {@code BitSet}
1078      * that is equal to it.
1079      * The clone of the bit set is another bit set that has exactly the
1080      * same bits set to {@code true} as this bit set.
1081      *
1082      * @return a clone of this bit set
1083      * @see    #size()
1084      */
1085     @Override
1086     public BitSet clone() {
1087         if (! sizeIsSticky)
1088             trimToSize();
1089 
1090         try {
1091             @SuppressWarnings("unchecked")
1092             BitSet result = (BitSet) super.clone();
1093             result.words = words.clone();
1094             result.checkInvariants();
1095             return result;
1096         } catch (CloneNotSupportedException e) {
1097             throw new InternalError(e);
1098         }
1099     }
1100 
1101     /**
1102      * Attempts to reduce internal storage used for the bits in this bit set.
1103      * Calling this method may, but is not required to, affect the value
1104      * returned by a subsequent call to the {@link #size()} method.
1105      */
1106     private void trimToSize() {
1107         if (wordsInUse != words.length) {
1108             words = Arrays.copyOf(words, wordsInUse);
1109             checkInvariants();
1110         }
1111     }