List of usage examples for java.math BigInteger divide
public BigInteger divide(BigInteger val)
From source file:org.apache.kylin.common.util.Bytes.java
/** * Iterate over keys within the passed range. *//*from w w w . ja v a 2 s .c om*/ public static Iterable<byte[]> iterateOnSplits(final byte[] a, final byte[] b, boolean inclusive, final int num) { byte[] aPadded; byte[] bPadded; if (a.length < b.length) { aPadded = padTail(a, b.length - a.length); bPadded = b; } else if (b.length < a.length) { aPadded = a; bPadded = padTail(b, a.length - b.length); } else { aPadded = a; bPadded = b; } if (compareTo(aPadded, bPadded) >= 0) { throw new IllegalArgumentException("b <= a"); } if (num <= 0) { throw new IllegalArgumentException("num cannot be <= 0"); } byte[] prependHeader = { 1, 0 }; final BigInteger startBI = new BigInteger(add(prependHeader, aPadded)); final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded)); BigInteger diffBI = stopBI.subtract(startBI); if (inclusive) { diffBI = diffBI.add(BigInteger.ONE); } final BigInteger splitsBI = BigInteger.valueOf(num + 1L); if (diffBI.compareTo(splitsBI) < 0) { return null; } final BigInteger intervalBI; try { intervalBI = diffBI.divide(splitsBI); } catch (Exception e) { LOG.error("Exception caught during division", e); return null; } final Iterator<byte[]> iterator = new Iterator<byte[]>() { private int i = -1; @Override public boolean hasNext() { return i < num + 1; } @Override public byte[] next() { i++; if (i == 0) return a; if (i == num + 1) return b; BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i))); byte[] padded = curBI.toByteArray(); if (padded[1] == 0) padded = tail(padded, padded.length - 2); else padded = tail(padded, padded.length - 1); return padded; } @Override public void remove() { throw new UnsupportedOperationException(); } }; return new Iterable<byte[]>() { @Override public Iterator<byte[]> iterator() { return iterator; } }; }
From source file:org.apache.hadoop.hbase.util.Bytes.java
/** * Iterate over keys within the passed range. *///from w w w. jav a2 s .c o m public static Iterable<byte[]> iterateOnSplits(final byte[] a, final byte[] b, boolean inclusive, final int num) { byte[] aPadded; byte[] bPadded; if (a.length < b.length) { aPadded = padTail(a, b.length - a.length); bPadded = b; } else if (b.length < a.length) { aPadded = a; bPadded = padTail(b, a.length - b.length); } else { aPadded = a; bPadded = b; } if (compareTo(aPadded, bPadded) >= 0) { throw new IllegalArgumentException("b <= a"); } if (num <= 0) { throw new IllegalArgumentException("num cannot be <= 0"); } byte[] prependHeader = { 1, 0 }; final BigInteger startBI = new BigInteger(add(prependHeader, aPadded)); final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded)); BigInteger diffBI = stopBI.subtract(startBI); if (inclusive) { diffBI = diffBI.add(BigInteger.ONE); } final BigInteger splitsBI = BigInteger.valueOf(num + 1); if (diffBI.compareTo(splitsBI) < 0) { return null; } final BigInteger intervalBI; try { intervalBI = diffBI.divide(splitsBI); } catch (Exception e) { LOG.error("Exception caught during division", e); return null; } final Iterator<byte[]> iterator = new Iterator<byte[]>() { private int i = -1; @Override public boolean hasNext() { return i < num + 1; } @Override public byte[] next() { i++; if (i == 0) return a; if (i == num + 1) return b; BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i))); byte[] padded = curBI.toByteArray(); if (padded[1] == 0) padded = tail(padded, padded.length - 2); else padded = tail(padded, padded.length - 1); return padded; } @Override public void remove() { throw new UnsupportedOperationException(); } }; return new Iterable<byte[]>() { @Override public Iterator<byte[]> iterator() { return iterator; } }; }
From source file:org.tvheadend.tvhclient.htsp.HTSService.java
private void onQueueStatus(HTSMessage msg) { TVHClientApplication app = (TVHClientApplication) getApplication(); Subscription sub = app.getSubscription(msg.getLong("subscriptionId")); if (sub == null) { return;/*from ww w.jav a 2 s. com*/ } if (msg.containsField("delay")) { BigInteger delay = msg.getBigInteger("delay"); delay = delay.divide(BigInteger.valueOf((1000))); sub.delay = delay.longValue(); } sub.droppedBFrames = msg.getLong("Bdrops", sub.droppedBFrames); sub.droppedIFrames = msg.getLong("Idrops", sub.droppedIFrames); sub.droppedPFrames = msg.getLong("Pdrops", sub.droppedPFrames); sub.packetCount = msg.getLong("packets", sub.packetCount); sub.queSize = msg.getLong("bytes", sub.queSize); app.updateSubscription(sub); }
From source file:com.lizardtech.expresszip.vaadin.ExportOptionsViewComponent.java
private void configureGridding() { GriddingOptions griddingOptions = new GriddingOptions(); ExportProps props = getExportProps(); griddingOptions.setExportWidth(props.getWidth()); griddingOptions.setExportHeight(props.getHeight()); griddingOptions.setGridding(gridCheckbox.booleanValue() && griddingDrawEnabled); if (griddingOptions.isGridding()) { if ((String) optGridOpt.getValue() == GRID_NUM_TILES) { griddingOptions.setGridMode(GriddingOptions.GridMode.DIVISION); int divX = Integer.parseInt(xTilesTextBox.getValue().toString()); int divY = Integer.parseInt(yTilesTextBox.getValue().toString()); griddingOptions.setDivX(divX > 0 ? divX : griddingOptions.getDivX()); griddingOptions.setDivY(divY > 0 ? divY : griddingOptions.getDivY()); } else { // GRID_TILE_DIMENSIONS or GRID_GROUND_DISTANCE griddingOptions.setGridMode(GriddingOptions.GridMode.METERS); if ((String) optGridOpt.getValue() == GRID_GROUND_DISTANCE) { Double groundResolution = getGroundResolution(); griddingOptions.setTileSizeX((int) (Double.parseDouble(getDistance_X()) / groundResolution)); griddingOptions.setTileSizeY((int) (Double.parseDouble(getDistance_Y()) / groundResolution)); } else { griddingOptions.setTileSizeX(Integer.parseInt(getTile_X())); griddingOptions.setTileSizeY(Integer.parseInt(getTile_Y())); }/*from w w w. ja va 2 s . co m*/ } } getExportProps().setGriddingOptions(griddingOptions); // update job summary text numTilesLabel.setValue(NUMBER_OF_TILES + griddingOptions.getNumTiles()); BigInteger size = griddingOptions.getExportSize(); String format = getImageFormat(); if (format.equals(PNG)) size = size.multiply(BigInteger.valueOf(85)).divide(BigInteger.valueOf(100)); else if (format.equals(JPEG)) size = size.divide(BigInteger.valueOf(15)); else if (format.equals(GIF)) size = size.multiply(BigInteger.valueOf(15)).divide(BigInteger.valueOf(100)); else if (format.equals(BMP)) size = size.multiply(BigInteger.valueOf(85)).divide(BigInteger.valueOf(100)); exportSizeEstimate.setValue(DISK_ESTIMATE + FileUtils.byteCountToDisplaySize(size)); for (ExportOptionsViewListener listener : listeners) listener.updateGridding(getExportProps()); }
From source file:com.flexive.core.storage.genericSQL.GenericTreeStorageSpreaded.java
/** * Helper function to create a new node. * * @param con an open and valid connection * @param seq reference to a sequencer * @param ce reference to the content engine * @param mode Live or Edit mode * @param parentNodeId the parent node (1=root) * @param name the name of the new node (only informative value) * @param label label for Caption property (only used if new reference is created) * @param position the position within the childs (0 based, Integer.MAX_VALUE may be used to * append to the end) * @param reference a reference to an existing content (must exist!) * @param data the optional data * @param nodeId the id to use or create a new one if < 0 * @param activateContent change the step of contents that have no live step to live in the max version? * @return the used or created node id/*from w w w. j av a 2s .com*/ * @throws FxTreeException if the function fails */ private long _createNode(Connection con, SequencerEngine seq, ContentEngine ce, FxTreeMode mode, long parentNodeId, String name, FxString label, int position, FxPK reference, String data, long nodeId, boolean activateContent) throws FxApplicationException { // acquire exclusive lock for parent node acquireLocksForUpdate(con, mode, Arrays.asList(parentNodeId)); // makeSpace(con, seq/*irrelevant*/, mode, parentNodeId, position/*irrelevant*/, 1); FxTreeNodeInfoSpreaded parentNode = (FxTreeNodeInfoSpreaded) getTreeNodeInfo(con, mode, parentNodeId); BigInteger boundaries[] = getBoundaries(con, parentNode, position); BigInteger leftBoundary = boundaries[0]; //== left border BigInteger rightBoundary = boundaries[1]; //== right border // Node has to be inserted between the left and right boundary and needs 2 slots for its left and right border BigInteger spacing = rightBoundary.subtract(leftBoundary).subtract(TWO); // Compute spacing for left,inner and right part spacing = spacing.divide(THREE); // We need at least 2 open slots (for the left and right boundary of the new node) //if the spacing is <= 0 we need more space if (spacing.compareTo(BigInteger.ZERO) <= 0/*less than*/) { throw new FxTreeException("ex.tree.create.noSpace", parentNodeId); } // try to use space more efficiently for flat structures, otherwise the first node of a folder // will get a third of the subtree space, the second one ninth, and so on. // Maxspacing indicates the number of nodes (*2) we expect to put in this node before space reorg spacing = spacing.compareTo(DEFAULT_NODE_SPACING) > 0 ? DEFAULT_NODE_SPACING : spacing; // final BigInteger left = leftBoundary.add(spacing).add(BigInteger.ONE); // don't add gap to left boundary (doesn't seem to have any benefits since that space is lost // unless the tree is reorganized anyway final BigInteger left = leftBoundary.add(BigInteger.ONE); final BigInteger right = left.add(spacing).add(BigInteger.ONE); NodeCreateInfo nci = getNodeCreateInfo(mode, seq, ce, nodeId, name, label, reference, activateContent); // Create the node PreparedStatement ps = null; try { ps = con.prepareStatement("INSERT INTO " + getTable(mode) + " (ID,PARENT,DEPTH,DIRTY,REF,LFT,RGT," + "CHILDCOUNT,NAME,MODIFIED_AT,TEMPLATE) VALUES " + "(" + nci.id + "," + parentNodeId + "," + (parentNode.getDepth() + 1) + ",?," + nci.reference.getId() + ",?,?,0,?," + StorageManager.getTimestampFunction() + ",?)"); ps.setBoolean(1, mode != FxTreeMode.Live); setNodeBounds(ps, 2, left); setNodeBounds(ps, 3, right); ps.setString(4, FxFormatUtils.escapeTreePath(nci.name)); if (StringUtils.isEmpty(data)) { ps.setNull(5, java.sql.Types.VARCHAR); } else { ps.setString(6, data); } ps.executeUpdate(); ps.close(); //update the parents childcount ps = con.prepareStatement( "UPDATE " + getTable(mode) + " SET CHILDCOUNT=CHILDCOUNT+1 WHERE ID=" + parentNodeId); ps.executeUpdate(); } catch (SQLException e) { throw new FxTreeException(LOG, e, "ex.db.sqlError", e.getMessage()); } finally { try { if (ps != null) ps.close(); } catch (Throwable t) { /*ignore*/ } } return nci.id; }
From source file:eu.dety.burp.joseph.attacks.bleichenbacher_pkcs1.BleichenbacherPkcs1DecryptionAttackExecutor.java
private void stepThree(final int i) throws Exception { BigInteger n = this.pubKey.getModulus(); BigInteger r;/*ww w . j a v a 2s . c om*/ BigInteger upperBound; BigInteger lowerBound; BigInteger max; BigInteger min; BigInteger[] tmp; ArrayList<Interval> ms = new ArrayList<>(15); for (Interval interval : this.m) { upperBound = step3ComputeUpperBound(this.si, n, interval.upper); lowerBound = step3ComputeLowerBound(this.si, n, interval.lower); r = lowerBound; // lowerBound <= r <= upperBound while (r.compareTo(upperBound) < 1) { // ceil((2*B+r*n)/si) max = (BigInteger.valueOf(2).multiply(this.bigB)).add(r.multiply(n)); tmp = max.divideAndRemainder(this.si); if (BigInteger.ZERO.compareTo(tmp[1]) != 0) { max = tmp[0].add(BigInteger.ONE); } else { max = tmp[0]; } // floor((3*B-1+r*n)/si min = BigInteger.valueOf(3).multiply(this.bigB); min = min.subtract(BigInteger.ONE); min = min.add(r.multiply(n)); min = min.divide(this.si); // build new interval if (interval.lower.compareTo(max) > 0) { max = interval.lower; } if (interval.upper.compareTo(min) < 0) { min = interval.upper; } if (max.compareTo(min) <= 0) { ms.add(new Interval(max, min)); } // one further.... r = r.add(BigInteger.ONE); } } loggerInstance.log(getClass(), " # of intervals for M" + i + ": " + ms.size(), Logger.LogLevel.INFO); if (ms.size() == 0) { throw new Exception("Zero intervals left, validity oracle seems to be wrong!"); } this.m = ms.toArray(new Interval[ms.size()]); }
From source file:net.pms.util.Rational.java
/** * Returns an instance that represents the value of {@code value}. * * @param value the value.//from w w w .ja v a 2 s.co m * @return An instance that represents the value of {@code value}. */ @Nullable public static Rational valueOf(@Nullable BigDecimal value) { if (value == null) { return null; } BigInteger numerator; BigInteger denominator; if (value.signum() == 0) { return ZERO; } if (BigDecimal.ONE.equals(value)) { return ONE; } if (value.scale() > 0) { BigInteger unscaled = value.unscaledValue(); BigInteger tmpDenominator = BigInteger.TEN.pow(value.scale()); BigInteger tmpGreatestCommonDivisor = unscaled.gcd(tmpDenominator); numerator = unscaled.divide(tmpGreatestCommonDivisor); denominator = tmpDenominator.divide(tmpGreatestCommonDivisor); } else { numerator = value.toBigIntegerExact(); denominator = BigInteger.ONE; } return new Rational(numerator, denominator, BigInteger.ONE, numerator, denominator); }
From source file:org.op4j.MiscTest.java
@Test public void test() throws Exception { final StopWatch watch = new StopWatch(); watch.start();/*from w ww . j av a 2 s . c o m*/ final String[] stringsArr1 = new String[] { "Hello", "Goodbye", null }; final List<String> stringsList1 = Arrays.asList(stringsArr1); final String[][] stringsStrings1 = new String[][] { new String[] { "Hello!", "Goodbye!" }, new String[] { "Hola!", "Adis!" } }; final List<String>[] stringsListStrings1 = (List<String>[]) new List<?>[] { Arrays.asList(new String[] { "Hello!", "Goodbye!" }), Arrays.asList(new String[] { "Hola!", "Adis!" }) }; final List<List<String>> stringsListStringsList1 = Arrays.asList(stringsListStrings1); final Map<String, String> map1 = new LinkedHashMap<String, String>(); map1.put("es", "Hola!"); map1.put("en", "Hello!"); map1.put("gl", "Ola!"); map1.put("ca", "Hola!"); final Map<String, String> map2 = new LinkedHashMap<String, String>(); map2.put("es", "Adis!"); map2.put("en", "Goodbye!"); map2.put("gl", "Adus!"); map2.put("ca", "Adu!"); map2.put("fr", "Adieu!"); final Map<String, String>[] maps1 = (Map<String, String>[]) new Map<?, ?>[] { map1, map2 }; System.out.println(Op.onList(stringsList1).get()); System.out.println(Op.onList(stringsList1).forEach().get()); final BigDecimal bd = new BigDecimal("3455234.6325"); final Float f = Float.valueOf(3455234.6325f); final Double d = Double.valueOf(3455234.6325); System.out.println(bd.doubleValue()); System.out.println(f); System.out.println(d); System.out.println( Op.onList(Arrays.asList(new Integer[] { 2, 3, 4, 1, 2, 2, 4, 5, 2, 3, 5, 6, 87, 45, 2, 3, 34, 1 })) .get()); System.out.println( Op.onList(Arrays.asList(new Integer[] { 2, 3, 4, 1, 2, 2, 4, 5, 2, 3, 5, 6, 87, 45, 2, 3, 34, 1 })) .distinct().get()); final List<List<String>> listOfListOfString1 = Arrays.asList((List<String>[]) new List<?>[] { Arrays.asList(new String[] { "a", "b", "a" }), Arrays.asList(new String[] { "a", "b", "a" }) }); final Set<Set<String>> setOfsetOfString1 = new LinkedHashSet<Set<String>>( Arrays.asList((Set<String>[]) new Set<?>[] { new LinkedHashSet<String>(Arrays.asList(new String[] { "a", "b", "a" })), new LinkedHashSet<String>(Arrays.asList(new String[] { "a", "b", "a" })) })); final String[][] arrayOfArrayOfString1 = new String[][] { new String[] { "a", "b", "a" }, new String[] { "a", "b", "a" } }; System.out.println(Op.onList(stringsList1).addAll("World!", "Mars!").get()); System.out.println(Op.onList(stringsList1).insertAll(1, "World!", "Mars!").get()); System.out.println(Op.onList(stringsList1).addAll(stringsList1).get()); System.out.println(Op.onList(stringsList1).get()); System.out.println(Op.onList(stringsList1).removeAllIndexes(0, 2).get()); System.out.println(Op.onList(stringsList1).removeAllIndexesNot(0).get()); System.out.println(Op.onList(stringsList1).removeAllIndexesNot(0, 2).get()); System.out.println(Op.onList(stringsList1).removeAllTrue(new IFunction<String, Boolean>() { public Boolean execute(String target, final ExecCtx ctx) { return Boolean.valueOf(target == null); } }).get()); System.out.println(Op.onList(stringsList1).removeAllNull().get()); System.out.println("================"); final Set<String> stringSet1 = new LinkedHashSet<String>(stringsList1); System.out.println(Op.onSet(stringSet1).addAll("World!", "Mars!").get()); System.out.println(Op.onSet(stringSet1).insertAll(1, "World!", "Mars!").get()); System.out.println(Op.onSet(stringSet1).addAll(stringsList1).get()); System.out.println("---"); System.out.println(Op.onSet(stringSet1).get()); System.out.println(Op.onSet(stringSet1).removeAllIndexes(0, 2).get()); System.out.println(Op.onSet(stringSet1).removeAllIndexesNot(0).get()); System.out.println(Op.onSet(stringSet1).removeAllIndexesNot(0, 2).get()); System.out.println(Op.onSet(stringSet1).removeAllNull().get()); System.out.println( printArray(Op.onArrayOf(Types.STRING, stringsArr1).insertAll(2, "lalero", "lururu").get())); System.out.println(Op.onMap(map1).put("fr", "All!").get()); System.out.println(Op.onMap(map1).insert(0, "fr", "All!").get()); System.out.println(Op.onMap(map1).insert(2, "fr", "All!").get()); System.out.println(Op.onMap(map2).get()); System.out.println(Op.onMap(map2).putAll(Op.onMap(map1).insert(0, "gl", "Meuuuu!").get()).get()); System.out.println(Op.onListFor(234, 12, 231)); System.out.println(Op.onListFor(234).addAll(10)); System.out.println(Op.onListFor(234).insert(0, 10)); System.out.println(Op.onListFor(234).addAll(10).insert(1, 3)); System.out.println(Op.onListFor(234).addAll(10).insert(1, 3).addAll((Integer) null)); System.out.println(Op.onListFor(234).addAll(10).insert(1, 3).addAll((Integer) null).removeAllNull()); System.out.println(Op.onListFor(234).addAll(10).insert(1, 3).removeAllIndexesNot(1)); System.out.println(printArray(Op.on(234).intoSingletonArrayOf(Types.INTEGER).addAll(8).get())); System.out.println(Op.on((List) null).addAll(123)); System.out.println(Op.on((Object) null).intoSingletonList().get()); System.out.println(Op.on((Object) null).intoSingletonSet().get()); System.out.println(printArray(Op.on((String) null).intoSingletonArrayOf(Types.STRING).addAll("a") .removeAllNull().removeAllIndexes(0).get())); // System.out.println(printArray(Op.buildArrayOfArray(Types.STRING).addAll(Op.buildArray(Types.STRING).addAll("a","b").get()).addAll(Op.buildArray(Types.STRING).addAll("1","2","3").get()).get())); // System.out.println(Op.buildMap(Types.INTEGER,Types.STRING).put(12,"hello!").get()); System.out.println(Op.onListFor("a", 1, "b", 3).couple().get()); System.out.println(Op.onListFor("hello", "goodbye", "adios", "ciao", "hola").sort().get()); System.out.println( Op.onListFor("hello", "goodbye", "adios", "ciao", "hola").toSet().sort(new Comparator<String>() { public int compare(String o1, String o2) { if (o1.length() < o2.length()) { return -1; } else if (o1.length() == o2.length()) { return 0; } return 1; } }).get()); System.out.println(printArray( Op.onListFor("hello", "goodbye", "adios", "ciao", "hola").toArrayOf(Types.STRING).sort().get())); System.out.println(printArray(Op.onListFor("hello", "goodbye", "adios", "ciao", "hola") .toArrayOf(Types.STRING).sort(new Comparator<String>() { public int compare(String o1, String o2) { if (o1.length() < o2.length()) { return -1; } else if (o1.length() == o2.length()) { return 0; } return 1; } }).get())); System.out.println( Op.on("12314123.4123").exec(FnString.toInteger(RoundingMode.CEILING, DecimalPoint.IS_POINT)).get()); System.out.println( Op.on("12314123.4123").exec(FnString.toInteger(RoundingMode.CEILING, DecimalPoint.IS_POINT)).get()); System.out.println(Op.on("12314123").exec(FnString.toInteger()).get()); System.out.println(Op.on("12314123").exec(FnString.toLong()).get()); System.out.println(Op.on("12314123").exec(FnString.toBigInteger()).get()); System.out.println(Op.on("12314123.4123").exec(FnString.toDouble()).get()); System.out.println(Op.on("12314123.4123").exec(FnString.toDouble(3, RoundingMode.CEILING)).get()); System.out.println(Op.on("12314123.4123").exec(FnString.toBigDecimal(3, RoundingMode.CEILING)).get()); final SimpleDateFormat dateFormat = new SimpleDateFormat(); System.out.println(dateFormat .format(Op.on(Calendar.getInstance()).exec(FnCalendar.truncate(Calendar.DATE)).get().getTime())); System.out.println(dateFormat .format(Op.on("25/nov/1979").exec(FnString.toCalendar("dd/MMM/yyyy", "es")).get().getTime())); // System.out.println(dateFormat.format(Op.onAll(1979, 11, 25, 12, 30).buildList().exec(ToCalendar.fromString("dd/MMM/yyyy", "es")).get().getTime())); System.out.println(dateFormat.format(Op.on(Op.onListFor(1979, 11, 25, 12, 30).get()) .exec(FnCalendar.fieldIntegerListToCalendar()).get().getTime())); System.out.println(dateFormat.format(Op.on(Op.onListFor("1979", "11", "25", "12", "30").get()) .toArrayOf(Types.STRING).exec(FnCalendar.fieldStringArrayToCalendar()).get().getTime())); System.out.println(Op.on(Op.onListFor(1979, 11, 25, 12, 30).get()).exec(FnList.ofInteger().sort()).get()); System.out.println(Op.on(Op.onSetFor(1979, 11, 25, 12, 30).get()).exec(FnSet.ofInteger().sort()).get()); System.out.println(Op.on(Op.onListFor(1979, 11, 25, 12, 30, 1980, 2, 43, 12, 11).get()) .exec(FnList.ofInteger().distinct()).get()); System.out.println(Op.on("hello").intoSingletonList().get()); System.out.println(printArray(Op.on("hello").intoSingletonArrayOf(Types.STRING).get())); // System.out.println(Op.buildList(Types.CALENDAR) // .addAll(Calendar.getInstance(), Calendar.getInstance()) // .forEach().exec(ToString.fromCalendar(DateStyle.FULL, TimeStyle.NONE, Locale.UK)).get()); // // System.out.println(Op.buildList(Types.CALENDAR) // .addAll(Calendar.getInstance(), Calendar.getInstance()) // .forEach().exec(ToString.fromCalendar(DateStyle.FULL, TimeStyle.SHORT, Locale.UK)).get()); // // System.out.println(Op.buildList(Types.CALENDAR) // .addAll(Calendar.getInstance(), Calendar.getInstance()) // .forEach().exec(ToString.fromCalendar("dd-MMM-yyyy", Locale.UK)).get()); // // System.out.println(Op.buildList(Types.CALENDAR) // .addAll(Calendar.getInstance(), Calendar.getInstance()) // .forEach().exec(ToString.fromCalendar("dd-MMMM-yyyy")).get()); // // System.out.println(Op.buildList(Types.DATE) // .addAll(new java.sql.Date(Calendar.getInstance().getTimeInMillis())) // .forEach().exec(ToString.fromDate("dd-MMM-yyyy", Locale.UK)).get()); // // // System.out.println(Op.buildList(Types.STRING) // .addAll(" Company ", " day ") // .forEach().exec(StringFuncs.trim()).get()); // System.out.println(Op.buildList(Types.STRING) // .addAll(" Company ", " day ") // .forEach().exec(StringFuncs.trim()).exec(StringFuncs.toUpperCase()).get()); System.out.println(Op.on("Dublin").exec(FnString.toHexadecimal(Charset.forName("ISO-8859-1"))) .exec(FnString.fromHexadecimal(Charset.forName("ISO-8859-1"))).get()); // System.out.println(Op.buildList(Types.NUMBER) // .addAll(45.9, new BigDecimal(34.456)) // .forEach().exec(ToString.fromCurrency(Locale.getDefault(), // 1, 2, 10, true)).get()); // System.out.println(Op.buildList(Types.NUMBER) // .addAll(45.9, 45, new BigDecimal(34.456)) // .forEach().exec(ToString.fromCurrency(Locale.getDefault(), // 1, 0, 0, true)).get()); // // System.out.println(Op.buildList(Types.NUMBER) // .addAll(45.9, 45, new BigDecimal(34.456), 0, 0.5, 0.211) // .forEach().exec(ToString.fromPercent(Locale.getDefault(), // 1, 0, 10, ',', '\'', false)).get()); System.out.println(Op.onArrayOf(Types.STRING, stringsArr1).toSet().get()); final List<String[]> listOfStringArray1 = new ArrayList<String[]>(); listOfStringArray1.add(Op.onListFor("Hola", "Hello", "Ciao", "Ola").toArrayOf(Types.STRING).get()); listOfStringArray1.add(Op.onListFor("Adios", "Goodbye", "Ciao", "Adus").toArrayOf(Types.STRING).get()); final List<Set<String>> listOfStringSet1 = new ArrayList<Set<String>>(); listOfStringSet1.add(Op.onListFor("Hola", "Hello", "Ciao", "Ola").toSet().get()); listOfStringSet1.add(Op.onListFor("Adios", "Goodbye", "Ciao", "Adus").toSet().get()); final Set<String[]> setOfStringArray1 = new LinkedHashSet<String[]>(); setOfStringArray1.add(Op.onListFor("Hola", "Hello", "Ciao", "Ola").toArrayOf(Types.STRING).get()); setOfStringArray1.add(Op.onListFor("Adios", "Goodbye", "Ciao", "Adus").toArrayOf(Types.STRING).get()); final Set<List<String>> setOfStringList1 = new LinkedHashSet<List<String>>(); setOfStringList1.add(Op.onArrayFor("Hola", "Hello", "Ciao", "Ola").toList().get()); setOfStringList1.add(Op.onArrayFor("Adios", "Goodbye", "Ciao", "Adus").toList().get()); final Set<Set<String>> setOfStringSet1 = new LinkedHashSet<Set<String>>(); setOfStringSet1.add(Op.onListFor("Hola", "Hello", "Ciao", "Ola").toSet().get()); setOfStringSet1.add(Op.onListFor("Adios", "Goodbye", "Ciao", "Adus").toSet().get()); System.out.println(Op.on("http://www.google.es/search?q=op4j&unusedParam=unusedValue '' 2^2 ") .exec(FnString.escapeJavaScript()).get()); System.out.println( Op.on("Body tag is written like \"<body>content here</body>\"").exec(FnString.escapeHTML()).get()); System.out.println("***___****___****"); System.out.println(Op.onList(stringsList1).forEach().ifNotNull().exec(FnString.toUpperCase()).get()); System.out.println("***___****___****"); System.out.println(Op.onList(listOfListOfString1).get()); // System.out.println(Op.onMap(map1).forEachEntry().exec(Ognl.forString("'in ' + #target.key + ' you say ' + #target.value")).get()); System.out.println(Op.onList(stringsList1).removeAllNull().sort().get()); // final List<Map<String,String>> listOfMapOfStringString1 = // Op.buildList(Types.MAP_OF_STRING_STRING).add(map1).add(map2).get(); // // System.out.println(printArray(Op.onListOfMap(listOfMapOfStringString1).toArrayOfMap().get())); System.out.println(Types.LIST_ITERATOR_OF_BOOLEAN.getSimpleName()); System.out.println(Op.onList(stringsList1).get()); System.out.println(Op.onList(stringsList1).forEach().replaceWith("op4j is great!").get()); System.out.println(Op.onList(stringsList1).forEach().replaceIfNullWith("op4j is great!").get()); System.out.println(printArray( Op.onArrayOf(Types.STRING, stringsArr1).forEach().replaceIfNullWith("op4j is great!").get())); System.out.println(printArray(Op.onArrayOf(Types.STRING, stringsArr1) .replaceWith(new String[] { "alpha", "beta" }).forEach().exec(FnString.toUpperCase()).get())); // System.out.println(Op.buildListOfList(Types.STRING).add(stringsList1).add(stringsList1).get()); // System.out.println(Op.buildListOfList(Types.STRING).addAll(stringsList1, stringsList1).get()); Op.on(Integer.valueOf(12)).exec(FnObject.intoSingletonArrayOf(Types.INTEGER)).get(); watch.stop(); System.out.println("TIME: " + watch.toString()); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); System.out.println("**********************"); List<String> list = Arrays.asList(new String[] { "12/10/1492", "6/12/1978", "15/07/2045", null }); Set<String> set1 = new LinkedHashSet<String>(list); Set<String> set2 = Op.on(list).toSet().get(); System.out.println("set1 : " + set1); System.out.println("set2 : " + set2); Set<Calendar> set3 = Op.on(list).toSet().forEach().exec(FnString.toCalendar("dd/MM/yyyy")).get(); System.out.println("set3asStr : " + Op.on(set3).map(FnCalendar.toStr("EEEE dd MMMM yyyy")).get()); Set<Calendar> set4 = Op.on(list).toSet().removeAllNull().forEach().exec(FnString.toCalendar("dd/MM/yyyy")) .get(); System.out.println( "set4asStr : " + Op.on(set4).map(FnCalendar.toStr("EEEE dd MMMM yyyy", new Locale("en"))).get()); Set<Calendar> set5 = Op.on(list).toSet().removeAllNull().map(FnString.toCalendar("dd/MM/yyyy")).get(); System.out.println( "set5asStr : " + Op.on(set5).map(FnCalendar.toStr("EEEE dd MMMM yyyy", new Locale("en"))).get()); Calendar now = Calendar.getInstance(); Set<Calendar> set6 = Op.on(list).toSet().map(FnString.toCalendar("dd/MM/yyyy")) .removeAllNullOrTrue(FnCalendar.after(now)).get(); System.out.println("set6asStr : " + Op.on(set6).map(FnCalendar.toStr("EEEE dd MMMM yyyy HH:mm:ss", new Locale("en"))).get()); // **************************** // WARNING: Non-op4j code!! // **************************** SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy"); Set<Calendar> set = new LinkedHashSet<Calendar>(); for (String element : list) { if (element != null) { Date date = null; try { date = dateFormat1.parse(element); } catch (ParseException e) { throw new RuntimeException(e); } Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(date.getTime()); if (!calendar.after(now)) { set.add(calendar); } } } System.out.println("setasStr : " + Op.on(set).map(FnCalendar.toStr("EEEE dd MMMM yyyy HH:mm:ss", new Locale("en"))).get()); Function<List<String>, Set<Calendar>> conversionFunction = Fn.onListOf(Types.STRING).toSet() .map(FnString.toCalendar("dd/MM/yyyy")).removeAllNullOrTrue(FnCalendar.after(now)).get(); System.out.println("setFnasStr : " + Op.on(conversionFunction.execute(list)) .map(FnCalendar.toStr("EEEE dd MMMM yyyy HH:mm:ss", new Locale("en"))).get()); int[] v1 = new int[] { 3, 2, 1, 3 }; long[] v2 = new long[] { 3, 2, 1, 3 }; Op.on(1).get(); List<Integer> iL = Op.onListFor(2, 1, 4, 213).get(); System.out.println(iL); System.out.println(Arrays.asList(Op.on(v1).forEach().exec(FnInteger.add(Integer.valueOf(10))).get())); Op.on(v2).forEach().get(); Op.on(123).exec(new IFunction<Integer, String>() { public String execute(Integer input, ExecCtx ctx) throws Exception { return "The input number is: " + input; } }).get(); System.out.println(Op.on(23.24).exec(FnDouble.add(43)).get()); System.out.println(Op.on(BigDecimal.valueOf(23.24)).exec(FnBigDecimal.add(1.2)).get()); System.out.println(Op.onListFor(30, 30, 40).map(FnNumber.toBigInteger()) .exec(FnBigInteger.avg(RoundingMode.CEILING)).get()); System.out.println(Op.on(10).exec(FnInteger.divideBy(3, RoundingMode.CEILING)).get()); Function<Integer, Boolean> fnAnd1 = FnBoolean.and(FnObject.eq("lala"), FnNumber.notEq(534)); System.out.println(Op.on(233).exec(FnBoolean.and(FnNumber.greaterThan(44), FnObject.lessThan(534))).get()); System.out.println( Op.on(1233).ifTrue(FnBoolean.not(FnBoolean.and(FnNumber.greaterThan(44), FnObject.lessThan(534)))) .exec(FnInteger.add(10)).get()); System.out.println(Op.on(1233).exec(FnFunc.chain(FnInteger.add(10), FnNumber.greaterThan(1200))).get()); System.out.println(Op.onListFor(1, 2, 3, 4).exec(FnList.ofInteger().containsAny(12, 3)).get()); String[] arr231 = new String[] { "be", "a", "31aa", "31_l", "31A" }; System.out.println(Arrays.asList(FnArray.ofString().sort().execute(arr231))); System.out .println(Arrays.asList(FnArray.ofString().sortBy(Call.methodForInteger("length")).execute(arr231))); System.out .println(FnList.ofString().sortBy(Call.methodForInteger("length")).execute(Arrays.asList(arr231))); String[] datesStr = new String[] { "12-10-1492", "06-12-1978" }; List<Calendar> dates = Op.on(datesStr).toList().map(FnString.toCalendar("dd-MM-yyyy")).get(); System.out.println(Op.on(dates).map(FnCalendar.toStr("yyyy, MMMM dd", new Locale("gl", "ES"))).get()); Function<Integer, Boolean> afnb1 = new Function<Integer, Boolean>() { public Boolean execute(Integer input, ExecCtx ctx) throws Exception { return Boolean.TRUE; } }; Function<Number, Boolean> afnb2 = new Function<Number, Boolean>() { public Boolean execute(Number input, ExecCtx ctx) throws Exception { return Boolean.TRUE; } }; Function<Integer, Boolean> afnb = FnBoolean.and(afnb1, afnb2); Function<Number, Boolean> bfnb1 = new Function<Number, Boolean>() { public Boolean execute(Number input, ExecCtx ctx) throws Exception { return Boolean.TRUE; } }; Function<Integer, Boolean> bfnb2 = new Function<Integer, Boolean>() { public Boolean execute(Integer input, ExecCtx ctx) throws Exception { return Boolean.TRUE; } }; Function<Integer, Boolean> bfnb = FnBoolean.and(bfnb1, bfnb2); Op.on(231).ifTrue(afnb).get(); Op.on(231).ifTrue(bfnb).get(); Op.on(231).ifTrue(FnBoolean.and(afnb1, afnb2)).get(); Op.on(231).ifTrue(FnBoolean.and(bfnb1, bfnb2)).get(); Function<Object, Boolean> cfnb1 = new Function<Object, Boolean>() { public Boolean execute(Object input, ExecCtx ctx) throws Exception { return Boolean.TRUE; } }; Function<Number, Boolean> cfnb2 = new Function<Number, Boolean>() { public Boolean execute(Number input, ExecCtx ctx) throws Exception { return Boolean.TRUE; } }; Function<Number, Boolean> cfnb = FnBoolean.and(cfnb1, cfnb2); Function<Number, Boolean> dfnb1 = new Function<Number, Boolean>() { public Boolean execute(Number input, ExecCtx ctx) throws Exception { return Boolean.TRUE; } }; Function<Object, Boolean> dfnb2 = new Function<Object, Boolean>() { public Boolean execute(Object input, ExecCtx ctx) throws Exception { return Boolean.TRUE; } }; Function<Number, Boolean> dfnb = FnBoolean.and(dfnb1, dfnb2); Op.on(231.2).ifTrue(cfnb).get(); Op.on(231.1).ifTrue(dfnb).get(); Op.on(231.2).ifTrue(FnBoolean.and(cfnb1, cfnb2)).get(); Op.on(231.1).ifTrue(FnBoolean.and(dfnb1, dfnb2)).get(); Function<Number, Integer> fnz1 = new Function<Number, Integer>() { public Integer execute(Number input, ExecCtx ctx) throws Exception { // TODO Auto-generated method stub return null; } }; Function<Integer, Integer> fnn1 = FnFunc.ifTrueThen(Types.INTEGER, FnNumber.greaterThan(2), fnz1); Fn.on(Types.INTEGER).exec(FnFunc.ifTrueThen(Types.NUMBER, FnNumber.greaterThan(2), fnz1)).get(); BigInteger biValue = BigInteger.valueOf(-1256565646); System.out.println("Starting value = " + biValue); BigInteger biOpResult = Op.on(biValue).exec(FnBigInteger.divideBy(BigInteger.valueOf(23))) .exec(FnBigInteger.pow(3)).exec(FnBigInteger.subtract(BigInteger.valueOf(5))) .exec(FnBigInteger.abs()).get(); System.out.println("With op4j: " + biOpResult); BigInteger biNorResult = biValue.divide(BigInteger.valueOf(23)).pow(3).subtract(BigInteger.valueOf(5)) .abs(); System.out.println("Without op4j: " + biNorResult); BigInteger biOpResult1 = Op.on(biValue).exec(FnBigInteger.divideBy(BigInteger.valueOf(23))).get(); System.out.println("[1] With op4j: " + biOpResult1); BigInteger biNorResult1 = biValue.divide(BigInteger.valueOf(23)); System.out.println("[1] Without op4j: " + biNorResult1); BigDecimal biOpResult1d = Op.on(new BigDecimal(biValue)) .exec(FnBigDecimal.divideBy(BigDecimal.valueOf(23.0), RoundingMode.DOWN)).get(); System.out.println("[1D] With op4j: " + biOpResult1d); BigDecimal biNorResult1d = new BigDecimal(biValue).divide(BigDecimal.valueOf(23.0), RoundingMode.DOWN); System.out.println("[1D] Without op4j: " + biNorResult1d); System.out .println( Op.on(Types.STRING, null) .exec(FnFunc.ifTrueThen(Types.STRING, FnBoolean.or(FnObject.isNull(), FnBoolean.and(FnObject.isNotNull(), FnFunc.chain(FnString.toInteger(), FnNumber.greaterThan(100)))), FnObject.replaceWith("lelo"))) .get()); System.out.println(Arrays.asList(Op.onArrayFor(4, 2).get())); System.out.println(Op.on("hello").zipKey(98).get()); System.out.println(Op.onListFor("en", "en", "es", "gl", "fr") .zipAndGroupValues("hello", "goodbye", "hola", "ola", "all").get()); System.out.println(Op.onListFor("hello", "goodbye", "hola", "ola", "all") .zipAndGroupKeys("en", "en", "es", "gl", "fr").get()); System.out.println(Op.onArrayFor("hello", "goodbye", "hola", "ola", "all") .zipAndGroupKeys("en", "en", "es", "gl", "fr").get()); System.out.println(Op.onMapFor(23, "twenty-three").and(43, "forty-three").and(10, "ten").sort().get()); System.out.println(Arrays.asList(Op.onArrayFor(1, 2, 1, 2, 2) .zipAndGroupValues(Types.STRING, "a", "b", "c", "d", "e").get().get(1))); System.out.println( Op.on("hello").ifTrue(FnString.notEq("uncapitalizable")).exec(FnString.toUpperCase()).get()); System.out.println(Op.on("uncapitalizable").ifTrue(FnString.notEq("uncapitalizable")) .exec(FnString.toUpperCase()).get()); Map<String, Integer> agesByName = Op.onListFor(27, 49, 19).zipKeys("John", "Mary", "Derek").get(); System.out.println(agesByName); Map<String, String> capitals = Op .onListFor("Spain", "Madrid", "United Kingdom", "London", "France", "Paris").couple().get(); System.out.println(capitals); String date = "06/12/1978"; Calendar cal = Op.on(date).exec(FnString.toCalendar("dd/MM/yyyy")).get(); System.out.println(dateFormat.format(cal.getTime())); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date dt = sdf.parse("06/12/1978"); Calendar c = Calendar.getInstance(); c.setTime(dt); System.out.println(dateFormat.format(c.getTime())); System.out.println(Op.onListFor(2, 12, 42, 5, 9, 24) .mapIfTrue(FnNumber.lessOrEqTo(10), FnObject.replaceWith("<"), FnObject.replaceWith(">")).get()); System.out.println(Op.on("LO E I N OR! ae").exec(FnString.asciify()).get()); System.out.println(Op.on(" a nu NU nu NU ").exec(FnString.asciify()).get()); System.out.println(Op.on("").exec(FnString.asciify()).get()); Pair<Integer, Integer> p = Op.on(dt).exec(FnTuple.pairWith(Call.i("getYear"), Get.i("month"))).get(); System.out.println(p); Integer i10 = Op.on(dt).exec(FnTuple.pairWith(Call.i("getYear"), Get.i("month"))) .exec(FnTuple.getValue1Of(Types.INTEGER)).get(); System.out.println(i10); }
From source file:net.pms.util.Rational.java
/** * Calculates the least common multiple for two {@link BigInteger}s using the formula * {@code u * v / gcd(u, v)} where {@code gcd} is the greatest common divisor for the two. * * @param u the first number.//from w ww.j a va 2 s .c om * @param v the second number. * @return The LCM, always 1 or greater. */ @Nullable public static BigInteger calculateLeastCommonMultiple(@Nullable BigInteger u, @Nullable BigInteger v) { if (u == null || v == null) { return null; } if (u.signum() == 0 && v.signum() == 0) { return BigInteger.ONE; } u = u.abs(); v = v.abs(); if (u.signum() == 0) { return v; } if (v.signum() == 0) { return u; } return u.divide(calculateGreatestCommonDivisor(u, v)).multiply(v); }
From source file:net.pms.util.Rational.java
/** * Returns an instance with the given {@code numerator} and * {@code denominator}.//w w w. j a va 2 s .co m * * @param numerator the numerator. * @param denominator the denominator. * @return An instance that represents the value of {@code numerator}/ * {@code denominator}. */ @Nullable public static Rational valueOf(@Nullable BigDecimal numerator, @Nullable BigDecimal denominator) { if (numerator == null || denominator == null) { return null; } if (numerator.signum() == 0 && denominator.signum() == 0) { return NaN; } if (denominator.signum() == 0) { return numerator.signum() > 0 ? POSITIVE_INFINITY : NEGATIVE_INFINITY; } if (numerator.signum() == 0) { return ZERO; } if (numerator.equals(denominator)) { return ONE; } if (denominator.signum() < 0) { numerator = numerator.negate(); denominator = denominator.negate(); } int scale = Math.max(numerator.scale(), denominator.scale()); if (scale > 0) { numerator = numerator.scaleByPowerOfTen(scale); denominator = denominator.scaleByPowerOfTen(scale); } BigInteger biNumerator = numerator.toBigIntegerExact(); BigInteger biDenominator = denominator.toBigIntegerExact(); BigInteger reducedNumerator; BigInteger reducedDenominator; BigInteger greatestCommonDivisor = calculateGreatestCommonDivisor(biNumerator, biDenominator); if (BigInteger.ONE.equals(greatestCommonDivisor)) { reducedNumerator = biNumerator; reducedDenominator = biDenominator; } else { reducedNumerator = biNumerator.divide(greatestCommonDivisor); reducedDenominator = biDenominator.divide(greatestCommonDivisor); } return new Rational(biNumerator, biDenominator, greatestCommonDivisor, reducedNumerator, reducedDenominator); }