1 2 12 package com.versant.core.common; 13 14 import com.versant.core.metadata.ClassMetaData; 15 import com.versant.core.metadata.ModelMetaData; 16 import com.versant.core.metadata.MDStatics; 17 18 22 public final class CmdBitSet { 23 private static final ClassMetaData[] EMPTY_CMDS = new ClassMetaData[0]; 24 private final int[] bitSet; 25 private final ModelMetaData jmd; 26 private int size; 27 private boolean cacheble = true; 28 29 public CmdBitSet(ModelMetaData jmd) { 30 bitSet = new int[(jmd.classes.length / 32) + 1]; 31 this.jmd = jmd; 32 } 33 34 public boolean isCacheble() { 35 return cacheble; 36 } 37 38 41 public void addPlus(ClassMetaData cmd) { 42 if ((bitSet[(cmd.index / 32)] & (1 << (cmd.index % 32))) == 0) { 43 bitSet[(cmd.index / 32)] |= (1 << (cmd.index % 32)); 44 if (cmd.cacheStrategy == MDStatics.CACHE_STRATEGY_NO) cacheble = false; 45 size++; 46 } 47 if (cmd.pcSubclasses != null) { 48 final ClassMetaData[] subs = cmd.pcSubclasses; 49 for (int k = subs.length - 1; k >= 0; k--) { 50 addPlus(subs[k]); 51 } 52 } 53 } 54 55 58 public boolean add(ClassMetaData cmd) { 59 if ((bitSet[(cmd.index / 32)] & (1 << (cmd.index % 32))) == 0) { 60 bitSet[(cmd.index / 32)] |= (1 << (cmd.index % 32)); 61 if (cmd.cacheStrategy == MDStatics.CACHE_STRATEGY_NO) cacheble = false; 62 size++; 63 return true; 64 } 65 return false; 66 } 67 68 public boolean remove(ClassMetaData cmd) { 69 if ((bitSet[(cmd.index / 32)] & (1 << (cmd.index % 32))) == 0) { 70 bitSet[(cmd.index / 32)] ^= (1 << (cmd.index % 32)); 71 size--; 72 return true; 73 } 74 return false; 75 } 76 77 80 public ClassMetaData[] toArray() { 81 if (size == 0) return EMPTY_CMDS; 82 final ClassMetaData[] cmds = new ClassMetaData[size]; 83 int count = 0; 84 for (int i = 0; i < bitSet.length; i++) { 85 int val = bitSet[i]; 86 for (int bit = 0; bit < 32; bit++) { 87 if ((val & (1 << bit)) != 0) { 88 cmds[count++] = jmd.classes[bit + (i * 32)]; 89 if (count == size) return cmds; 90 } 91 } 92 } 93 return cmds; 94 } 95 96 99 public int[] getIndexes() { 100 if (size == 0) return null; 101 final int[] indexes = new int[size]; 102 int count = 0; 103 for (int i = 0; i < bitSet.length; i++) { 104 int val = bitSet[i]; 105 for (int bit = 0; bit < 32; bit++) { 106 if ((val & (1 << bit)) != 0) { 107 indexes[count++] = bit + (i * 32); 108 if (count == size) return indexes; 109 } 110 } 111 } 112 return indexes; 113 } 114 115 118 public ClassMetaData[] toArrayIfCacheble() { 119 if (!cacheble) return null; 120 if (size == 0) return null; 121 final ClassMetaData[] cmds = new ClassMetaData[size]; 122 int count = 0; 123 for (int i = 0; i < bitSet.length; i++) { 124 int val = bitSet[i]; 125 for (int bit = 0; bit < 32; bit++) { 126 if ((val & (1 << bit)) != 0) { 127 ClassMetaData cmd = jmd.classes[bit + (i * 32)]; 128 if (cmd.cacheStrategy == MDStatics.CACHE_STRATEGY_NO) return null; 129 cmds[count++] = cmd; 130 if (count == size) return cmds; 131 } 132 } 133 } 134 return cmds; 135 } 136 137 public void clear() { 138 for (int i = bitSet.length - 1; i >= 0; i--) { 139 bitSet[i] = 0; 140 } 141 size = 0; 142 } 143 144 public boolean contains(ClassMetaData cmd) { 145 return (bitSet[(cmd.index / 32)] & (1 << (cmd.index % 32))) != 0; 146 } 147 148 public boolean containsAny(int[] bits) { 149 return CmdBitSet.containsAny(bitSet, bits); 150 } 151 152 public static boolean contains(ClassMetaData cmd, int[] bits) { 153 return (bits[(cmd.index / 32)] & (1 << (cmd.index % 32))) != 0; 154 } 155 156 public boolean contains(ClassMetaData[] cmds) { 157 for (int i = cmds.length - 1; i >= 0; i--) { 158 if (contains(cmds[i])) return true; 159 } 160 return false; 161 } 162 163 public static boolean contains(ClassMetaData[] cmds, int[] bits) { 164 for (int i = cmds.length - 1; i >= 0; i--) { 165 if (contains(cmds[i], bits)) return true; 166 } 167 return false; 168 } 169 170 public static int[] createFor(ClassMetaData[] cmds, ModelMetaData jdm) { 171 final CmdBitSet cmdBits = new CmdBitSet(jdm); 172 if (cmds != null) { 173 for (int i = 0; i < cmds.length; i++) { 174 cmdBits.add(cmds[i]); 175 } 176 } 177 return cmdBits.getBits(); 178 } 179 180 184 public static boolean containsAny(int[] src, int[] data) { 185 for (int i = 0; i < src.length; i++) { 186 if ((src[i] & data[i]) != 0) return true; 187 } 188 return false; 189 } 190 191 195 public int[] getBits() { 196 return bitSet; 197 } 198 } 199 | Popular Tags |