List of usage examples for java.util BitSet toString
public String toString()
From source file:Main.java
public static void main(String[] args) { BitSet bitset1 = new BitSet(8); BitSet bitset2 = new BitSet(8); // assign values to bitset1 bitset1.set(0);/* w ww . j a v a 2 s. c o m*/ bitset1.set(1); bitset1.set(2); // assign values to bitset2 bitset2.set(2); bitset2.set(4); bitset2.set(6); // print the sets System.out.println("Bitset1:" + bitset1); System.out.println("Bitset2:" + bitset2); // print the string representations of the bitsets System.out.println(bitset1.toString()); System.out.println(bitset2.toString()); }
From source file:hivemall.ftvec.ranking.ItemPairsSamplingUDTF.java
private void sampleWithReplacement(final int numPosItems, final int numNegItems, @Nonnull final BitSet bitset) throws HiveException { final int numSamples = Math.max(1, Math.round(numPosItems * samplingRate)); for (int s = 0; s < numSamples; s++) { int nth = _rand.nextInt(numPosItems); int i = BitUtils.indexOfSetBit(bitset, nth); if (i == -1) { throw new UDFArgumentException("Cannot find a value for " + nth + "-th element in bitset " + bitset.toString() + " where numPosItems = " + numPosItems); }/*from w ww .j av a2 s . c o m*/ nth = _rand.nextInt(numNegItems); int j = BitUtils.indexOfClearBit(bitset, nth, maxItemId); if (j < 0 || j > maxItemId) { throw new UDFArgumentException("j MUST be in [0," + maxItemId + "] but j was " + j); } posItemId.set(i); negItemId.set(j); forward(forwardObjs); } }
From source file:hivemall.ftvec.ranking.ItemPairsSamplingUDTF.java
private void sampleWithoutReplacement(int numPosItems, int numNegItems, @Nonnull final BitSet bitset) throws HiveException { final BitSet bitsetForPosSampling = bitset; final BitSet bitsetForNegSampling = BitSet.valueOf(bitset.toLongArray()); final int numSamples = Math.max(1, Math.round(numPosItems * samplingRate)); for (int s = 0; s < numSamples; s++) { int nth = _rand.nextInt(numPosItems); int i = BitUtils.indexOfSetBit(bitsetForPosSampling, nth); if (i == -1) { throw new UDFArgumentException("Cannot find a value for " + nth + "-th element in bitset " + bitset.toString() + " where numPosItems = " + numPosItems); }/*from w w w .ja v a 2 s . com*/ bitsetForPosSampling.set(i, false); --numPosItems; nth = _rand.nextInt(numNegItems); int j = BitUtils.indexOfClearBit(bitsetForNegSampling, nth, maxItemId); if (j < 0 || j > maxItemId) { throw new UDFArgumentException("j MUST be in [0," + maxItemId + "] but j was " + j); } bitsetForNegSampling.set(j, true); --numNegItems; posItemId.set(i); negItemId.set(j); forward(forwardObjs); if (numPosItems <= 0) { // cannot draw a positive example anymore return; } else if (numNegItems <= 0) { // cannot draw a negative example anymore return; } } }
From source file:org.apache.hawq.pxf.service.utilities.AnalyzeUtils.java
/** * Marks sampleSize bits out of the poolSize, in a uniform way. * * @param poolSize pool size/*ww w .j a v a 2 s . c o m*/ * @param sampleSize sample size * @return bit set with sampleSize bits set out of poolSize. */ static public BitSet generateSamplingBitSet(int poolSize, int sampleSize) { int skip = 0, chosen = 0, curIndex = 0; BitSet bitSet = new BitSet(); if (poolSize <= 0 || sampleSize <= 0) { return bitSet; } if (sampleSize >= poolSize) { LOG.debug("sampling bit map has " + poolSize + " elements (100%)"); bitSet.set(0, poolSize); return bitSet; } skip = (poolSize / sampleSize) + 1; while (chosen < sampleSize) { bitSet.set(curIndex); chosen++; if (chosen == sampleSize) { break; } for (int i = 0; i < skip; ++i) { curIndex = nextClearBitModulo((++curIndex) % poolSize, poolSize, bitSet); if (curIndex == -1) { // should never happen throw new IllegalArgumentException("Trying to sample more than pool size " + "(pool size " + poolSize + ", sampling size " + sampleSize); } } } LOG.debug("sampling bit map has " + chosen + " elements:" + bitSet.toString()); return bitSet; }
From source file:org.jpos.iso.ISOBasePackager.java
/** * @param m/* ww w. j ava2 s . c om*/ * the Component to pack * @return Message image * @exception ISOException */ public byte[] pack(ISOComponent m) throws ISOException { LogEvent evt = null; if (logger != null) evt = new LogEvent(this, "pack"); try { if (m.getComposite() != m) throw new ISOException("Can't call packager on non Composite"); ISOComponent c; ArrayList<byte[]> v = new ArrayList<byte[]>(128); Map fields = m.getChildren(); int len = 0; int first = getFirstField(); c = (ISOComponent) fields.get(0); byte[] b; // if (m instanceof ISOMsg && headerLength > 0) { // byte[] h = ((ISOMsg) m).getHeader(); // if (h != null) // len += h.length; // } if (first > 0 && c != null) { b = fld[0].pack(c); len += b.length; v.add(b); } if (emitBitMap()) { // BITMAP (-1 in HashTable) c = (ISOComponent) fields.get(-1); b = getBitMapfieldPackager().pack(c); len += b.length; v.add(b); if (logger != null) { BitSet bmap = (BitSet) c.getValue(); evt.addMessage("<bitmap>" + bmap.toString() + "</bitmap>"); } } // if Field 1 is a BitMap then we are packing an // ISO-8583 message so next field is fld#2. // else we are packing an ANSI X9.2 message, first field is 1 int tmpMaxField = Math.min(m.getMaxField(), 128); for (int i = first; i <= tmpMaxField; i++) { if ((c = (ISOComponent) fields.get(i)) != null) { try { ISOFieldPackager fp = fld[i]; if (fp == null) throw new ISOException("null field " + i + " packager"); b = fp.pack(c); len += b.length; v.add(b); if (logger != null) { String value = c.getValue() + ""; evt.addMessage("<pack fld=\"" + i + "\" packager=\"" + fld[i].getClass().getName() + "\" name=\"" + fld[i].getDescription() + "\">"); if (c.getValue() instanceof ISOMsg) evt.addMessage(c.getValue()); else if (c.getValue() instanceof byte[]) { evt.addMessage(" <value type='binary'>" + ISOUtil.hexString((byte[]) c.getValue()) + "</value>"); } else { if (i == 2) { value = simpleEncodeCardNo(value); } evt.addMessage(" <value>" + value + "</value>"); } evt.addMessage("</pack>"); } } catch (ISOException e) { if (evt != null) { evt.addMessage("error packing field " + i); evt.addMessage(c); evt.addMessage(e); } throw new ISOException("error packing field " + i, e); } } } if (m.getMaxField() > 128 && fld.length > 128) { for (int i = 1; i <= 64; i++) { if ((c = (ISOComponent) fields.get(i + 128)) != null) { try { b = fld[i + 128].pack(c); len += b.length; v.add(b); } catch (ISOException e) { if (evt != null) { evt.addMessage("error packing field " + (i + 128)); evt.addMessage(c); evt.addMessage(e); } throw e; } } } } int k = 0; byte[] d = new byte[len]; // if ISOMsg insert header // if (m instanceof ISOMsg && headerLength > 0) { // byte[] h = ((ISOMsg) m).getHeader(); // if (h != null) { // System.arraycopy(h, 0, d, k, h.length); // k += h.length; // } // } for (byte[] bb : v) { System.arraycopy(bb, 0, d, k, bb.length); k += bb.length; } if (evt != null) // save a few CPU cycle if no logger available evt.addMessage(CardNoEncodeUtil.encodeCardNo((ISOUtil.hexString(d)))); return d; } catch (ISOException e) { if (evt != null) evt.addMessage(e); throw e; } finally { if (evt != null) Logger.log(evt); } }
From source file:org.jpos.iso.ISOBasePackager.java
/** * @param m//from ww w .ja v a 2 s . c om * the Container of this message * @param b * ISO message image * @return consumed bytes * @exception ISOException */ public int unpack(ISOComponent m, byte[] b) throws ISOException { LogEvent evt = new LogEvent(this, "unpack"); int consumed = 0; try { if (m.getComposite() != m) throw new ISOException("Can't call packager on non Composite"); if (logger != null) // save a few CPU cycle if no logger available evt.addMessage(CardNoEncodeUtil.encodeCardNo((ISOUtil.hexString(b)))); // if ISOMsg and headerLength defined // if (m instanceof ISOMsg /* && ((ISOMsg) m).getHeader()==null */ // && headerLength > 0) { // byte[] h = new byte[headerLength]; // System.arraycopy(b, 0, h, 0, headerLength); // ((ISOMsg) m).setHeader(h); // consumed += headerLength; // } if (!(fld[0] == null) && !(fld[0] instanceof ISOBitMapPackager)) { ISOComponent mti = fld[0].createComponent(0); consumed += fld[0].unpack(mti, b, consumed); m.set(mti); } BitSet bmap = null; int maxField = fld.length; if (emitBitMap()) { ISOBitMap bitmap = new ISOBitMap(-1); consumed += getBitMapfieldPackager().unpack(bitmap, b, consumed); bmap = (BitSet) bitmap.getValue(); if (logger != null) evt.addMessage("<bitmap>" + bmap.toString() + "</bitmap>"); m.set(bitmap); maxField = Math.min(maxField, bmap.size()); } for (int i = getFirstField(); i < maxField; i++) { try { if (bmap == null && fld[i] == null) continue; if (maxField > 128 && i == 65) continue; // ignore extended bitmap if (bmap == null || bmap.get(i)) { if (fld[i] == null) throw new ISOException("field packager '" + i + "' is null"); ISOComponent c = fld[i].createComponent(i); consumed += fld[i].unpack(c, b, consumed); if (logger != null) { String value = c.getValue() + ""; evt.addMessage("<unpack fld=\"" + i + "\" packager=\"" + fld[i].getClass().getName() + "\" name=\"" + fld[i].getDescription() + "\">"); if (c.getValue() instanceof ISOMsg) evt.addMessage(c.getValue()); else if (c.getValue() instanceof byte[]) { evt.addMessage(" <value type='binary'>" + ISOUtil.hexString((byte[]) c.getValue()) + "</value>"); } else { if (i == 2) { value = simpleEncodeCardNo(value); } evt.addMessage(" <value>" + value + "</value>"); } evt.addMessage("</unpack>"); } m.set(c); } } catch (ISOException e) { evt.addMessage("error unpacking field " + i + " consumed=" + consumed); evt.addMessage(e); // jPOS-3 e = new ISOException(String.format("%s (%s) unpacking field=%d, consumed=%d", e.getMessage(), e.getNested().toString(), i, consumed)); throw e; } } if (b.length != consumed) { evt.addMessage("WARNING: unpack len=" + b.length + " consumed=" + consumed); } return consumed; } catch (ISOException e) { evt.addMessage(e); throw e; } catch (Exception e) { evt.addMessage(e); throw new ISOException(e.getMessage() + " consumed=" + consumed); } finally { Logger.log(evt); } }
From source file:org.jpos.iso.ISOBasePackager.java
public void unpack(ISOComponent m, InputStream in) throws IOException, ISOException { LogEvent evt = new LogEvent(this, "unpack"); try {//from ww w. j a va 2 s. co m if (m.getComposite() != m) throw new ISOException("Can't call packager on non Composite"); // if ISOMsg and headerLength defined // if (m instanceof ISOMsg && ((ISOMsg) m).getHeader() == null && headerLength > 0) { // byte[] h = new byte[headerLength]; // in.read(h, 0, headerLength); // ((ISOMsg) m).setHeader(h); // } if (!(fld[0] instanceof ISOMsgFieldPackager) && !(fld[0] instanceof ISOBitMapPackager)) { ISOComponent mti = fld[0].createComponent(0); fld[0].unpack(mti, in); m.set(mti); } BitSet bmap = null; int maxField = fld.length; if (emitBitMap()) { ISOBitMap bitmap = new ISOBitMap(-1); getBitMapfieldPackager().unpack(bitmap, in); bmap = (BitSet) bitmap.getValue(); if (logger != null) evt.addMessage("<bitmap>" + bmap.toString() + "</bitmap>"); m.set(bitmap); maxField = Math.min(maxField, bmap.size()); } for (int i = getFirstField(); i < maxField; i++) { if (bmap == null && fld[i] == null) continue; if (bmap == null || bmap.get(i)) { if (fld[i] == null) throw new ISOException("field packager '" + i + "' is null"); ISOComponent c = fld[i].createComponent(i); fld[i].unpack(c, in); if (logger != null) { evt.addMessage( "<unpack fld=\"" + i + "\" packager=\"" + fld[i].getClass().getName() + "\">"); if (c.getValue() instanceof ISOMsg) evt.addMessage(c.getValue()); else evt.addMessage(" <value>" + c.getValue().toString() + "</value>"); evt.addMessage("</unpack>"); } m.set(c); } } if (bmap != null && bmap.get(65) && fld.length > 128 && fld[65] instanceof ISOBitMapPackager) { bmap = (BitSet) ((ISOComponent) m.getChildren().get(65)).getValue(); for (int i = 1; i < 64; i++) { if (bmap == null || bmap.get(i)) { ISOComponent c = fld[i + 128].createComponent(i); fld[i + 128].unpack(c, in); if (logger != null) { evt.addMessage("<unpack fld=\"" + i + 128 + "\" packager=\"" + fld[i + 128].getClass().getName() + "\">"); evt.addMessage(" <value>" + c.getValue().toString() + "</value>"); evt.addMessage("</unpack>"); } m.set(c); } } } } catch (ISOException e) { evt.addMessage(e); throw e; } catch (EOFException e) { throw e; } catch (Exception e) { evt.addMessage(e); throw new ISOException(e); } finally { Logger.log(evt); } }
From source file:org.openecomp.sdnc.sli.aai.AAIRequest.java
public static void setProperties(Properties props, AAIService aaiService) { AAIRequest.configProperties = props; AAIRequest.aaiService = aaiService;//from w ww. java 2 s. c o m try { URL url = null; Bundle bundle = FrameworkUtil.getBundle(AAIServiceActivator.class); if (bundle != null) { BundleContext ctx = bundle.getBundleContext(); if (ctx == null) return; url = ctx.getBundle().getResource(AAIService.PATH_PROPERTIES); } else { url = aaiService.getClass().getResource("/aai-path.properties"); } InputStream in = url.openStream(); Reader reader = new InputStreamReader(in, "UTF-8"); Properties properties = new Properties(); properties.load(reader); LOG.info("loaded " + properties.size()); Set<String> keys = properties.stringPropertyNames(); int index = 0; Set<String> resourceNames = new TreeSet<String>(); for (String key : keys) { String[] tags = key.split("\\|"); for (String tag : tags) { if (!resourceNames.contains(tag)) { resourceNames.add(tag); tagValues.put(tag, Integer.toString(++index)); } } BitSet bs = new BitSet(256); for (String tag : tags) { String value = tagValues.get(tag); Integer bitIndex = Integer.parseInt(value); bs.set(bitIndex); } String path = properties.getProperty(key); LOG.info(String.format("bitset %s\t\t%s", bs.toString(), path)); bitsetPaths.put(bs, path); } LOG.info("loaded " + resourceNames.toString()); } catch (Exception e) { LOG.error("Caught exception", e); } }