Example usage for java.math BigInteger valueOf

List of usage examples for java.math BigInteger valueOf

Introduction

In this page you can find the example usage for java.math BigInteger valueOf.

Prototype

private static BigInteger valueOf(int val[]) 

Source Link

Document

Returns a BigInteger with the given two's complement representation.

Usage

From source file:com.dianping.puma.datahandler.AbstractDataHandler.java

protected Object convertUnsignedValueIfNeeded(int pos, Object value, TableMetaInfo tableMeta) {
    Object newValue = value;// w w  w .  j  a  v  a2 s. c om
    if (value != null) {
        switch (tableMeta.getRawTypeCodes().get(pos)) {
        case BinlogConstants.MYSQL_TYPE_TINY:
            if ((value instanceof Integer) && (Integer) value < 0 && !tableMeta.getSignedInfos().get(pos)) {
                newValue = Integer.valueOf((Integer) value + (1 << 8));
            }
            break;
        case BinlogConstants.MYSQL_TYPE_INT24:
            if ((value instanceof Integer) && (Integer) value < 0 && !tableMeta.getSignedInfos().get(pos)) {
                newValue = Integer.valueOf((Integer) value + (1 << 24));
            }
            break;
        case BinlogConstants.MYSQL_TYPE_SHORT:
            if ((value instanceof Integer) && (Integer) value < 0 && !tableMeta.getSignedInfos().get(pos)) {
                newValue = Integer.valueOf((Integer) value + (1 << 16));
            }
            break;
        case BinlogConstants.MYSQL_TYPE_INT:
            if ((value instanceof Integer) && (Integer) value < 0 && !tableMeta.getSignedInfos().get(pos)) {
                newValue = Long.valueOf((Integer) value) + (1L << 32);
            } else {
                if (value instanceof Integer) {
                    newValue = Long.valueOf((Integer) value);
                }
            }
            break;
        case BinlogConstants.MYSQL_TYPE_LONGLONG:
            if ((value instanceof Long) && (Long) value < 0 && !tableMeta.getSignedInfos().get(pos)) {
                newValue = BigInteger.valueOf((Long) value).add(BigInteger.ONE.shiftLeft(64));
            } else {
                if (value instanceof Long) {
                    newValue = BigInteger.valueOf((Long) value);
                }
            }
            break;
        default:
            break;
        }

    }
    return newValue;
}

From source file:burstcoin.jminer.core.checker.task.OCLCheckerFindAllBelowTargetTask.java

@Override
public void run() {
    int[] lowestNonces;
    synchronized (oclChecker) {
        // todo not working?!
        lowestNonces = oclChecker.findTarget(generationSignature, scoops, targetDeadline);
    }/*ww  w .j a  v a2 s . com*/

    if (lowestNonces != null) {
        List<DevPoolResult> devPoolResults = new ArrayList<>();
        for (int lowestNonce : lowestNonces) {
            long nonce = chunkPartStartNonce + lowestNonce;
            BigInteger result = calculateResult(scoops, generationSignature, lowestNonce);
            BigInteger deadline = result.divide(BigInteger.valueOf(baseTarget));
            long calculatedDeadline = deadline.longValue();

            devPoolResults.add(new DevPoolResult(blockNumber, calculatedDeadline, nonce, chunkPartStartNonce));
        }
        publisher.publishEvent(new CheckerDevResultEvent(blockNumber, chunkPartStartNonce, devPoolResults));
    } else {
        publisher.publishEvent(new CheckerDevResultEvent(blockNumber, chunkPartStartNonce, null));
    }
}

From source file:org.gridobservatory.greencomputing.dao.TimeseriesDao.java

protected void insertTimeSeriesAcquisitions(final BigInteger timeSeriesId,
        final List<TimeseriesAcquisitionType> acquisitions) {
    executor.submit(new Runnable() {
        @Override/*from   w  w w  . j  av a  2s.  co m*/
        public void run() {
            if (acquisitions != null) {
                getJdbcTemplate().batchUpdate(
                        "insert into time_series_acquisition (time_series_id, ts, value) values (?,?,?)",
                        new BatchPreparedStatementSetter() {

                            @Override
                            public void setValues(PreparedStatement ps, int i) throws SQLException {
                                ps.setLong(1, timeSeriesId.longValue());
                                long dateInMilis = acquisitions.get(i).getTs()
                                        .multiply(BigInteger.valueOf(1000l)).longValue();
                                ps.setTimestamp(2, new Timestamp(dateInMilis));
                                ps.setBigDecimal(3, new BigDecimal(acquisitions.get(i).getV()));
                            }

                            @Override
                            public int getBatchSize() {
                                return acquisitions.size();
                            }
                        });
            }
        }
    });
}

From source file:it.gualtierotesta.gdocx.GTc.java

/**
 * Set cell width/*from   www . j  a  v  a 2  s . c o  m*/
 *
 * @param lWidth width value (should be greater than 0)
 * @param sType type (unit) of the value (for ex. "dxa")
 * @return same GTc instance
 */
@Nonnull
public GTc width(final long lWidth, @Nonnull final String sType) {

    Validate.isTrue(0L < lWidth, "Width value not valid");
    Validate.notEmpty(sType, "Type not valid");

    final TblWidth cellWidth = FACTORY.createTblWidth();
    cellWidth.setType(sType);
    cellWidth.setW(BigInteger.valueOf(lWidth));
    tcPr.setTcW(cellWidth);
    return this;
}

From source file:co.rsk.peg.BridgeSerializationUtilsTest.java

@Test
public void serializeMapOfHashesToLong() throws Exception {
    PowerMockito.mockStatic(RLP.class);
    mock_RLP_encodeElement();//from   w w  w .  j av  a2s.  c o  m
    mock_RLP_encodeBigInteger();
    mock_RLP_encodeList();

    Map<Sha256Hash, Long> sample = new HashMap<>();
    sample.put(Sha256Hash.wrap(charNTimes('b', 64)), 1L);
    sample.put(Sha256Hash.wrap(charNTimes('d', 64)), 2L);
    sample.put(Sha256Hash.wrap(charNTimes('a', 64)), 3L);
    sample.put(Sha256Hash.wrap(charNTimes('c', 64)), 4L);

    byte[] result = BridgeSerializationUtils.serializeMapOfHashesToLong(sample);
    String hexResult = Hex.toHexString(result);
    StringBuilder expectedBuilder = new StringBuilder();
    char[] sorted = new char[] { 'a', 'b', 'c', 'd' };
    for (char c : sorted) {
        String key = charNTimes(c, 64);
        expectedBuilder.append("dd");
        expectedBuilder.append(key);
        expectedBuilder.append("ff");
        expectedBuilder
                .append(Hex.toHexString(BigInteger.valueOf(sample.get(Sha256Hash.wrap(key))).toByteArray()));
    }
    assertEquals(expectedBuilder.toString(), hexResult);
}

From source file:com.github.jrrdev.mantisbtsync.core.jobs.projects.ProjectsVersionsWriterTest.java

/**
 * Build the items to write./*  w  ww  .  java2  s.  c  om*/
 *
 * @return items
 */
private List<ProjectVersionData> buildItems() {
    final List<ProjectVersionData> items = new ArrayList<ProjectVersionData>();

    final ProjectVersionData item1 = new ProjectVersionData();
    item1.setId(BigInteger.ONE);
    item1.setName("new_version_1");
    item1.setProject_id(BigInteger.ONE);

    final ProjectVersionData item2 = new ProjectVersionData();
    item2.setId(BigInteger.valueOf(2));
    item2.setName("new_version_2");
    item2.setProject_id(BigInteger.ONE);

    items.add(item1);
    items.add(item2);

    return items;
}

From source file:org.crazydog.util.spring.NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 *
 * @param number      the number to convert
 * @param targetClass the target class to convert to
 * @return the converted number/*w w w. j a  va  2s  .c o m*/
 * @throws IllegalArgumentException if the target class is not supported
 *                                  (i.e. not a standard Number subclass as included in the JDK)
 * @see Byte
 * @see Short
 * @see Integer
 * @see Long
 * @see BigInteger
 * @see Float
 * @see Double
 * @see BigDecimal
 */
@SuppressWarnings("unchecked")
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)
        throws IllegalArgumentException {

    org.springframework.util.Assert.notNull(number, "Number must not be null");
    org.springframework.util.Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
        return (T) number;
    } else if (Byte.class == targetClass) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Byte(number.byteValue());
    } else if (Short.class == targetClass) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Short(number.shortValue());
    } else if (Integer.class == targetClass) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Integer(number.intValue());
    } else if (Long.class == targetClass) {
        BigInteger bigInt = null;
        if (number instanceof BigInteger) {
            bigInt = (BigInteger) number;
        } else if (number instanceof BigDecimal) {
            bigInt = ((BigDecimal) number).toBigInteger();
        }
        // Effectively analogous to JDK 8's BigInteger.longValueExact()
        if (bigInt != null && (bigInt.compareTo(LONG_MIN) < 0 || bigInt.compareTo(LONG_MAX) > 0)) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Long(number.longValue());
    } else if (BigInteger.class == targetClass) {
        if (number instanceof BigDecimal) {
            // do not lose precision - use BigDecimal's own conversion
            return (T) ((BigDecimal) number).toBigInteger();
        } else {
            // original value is not a Big* number - use standard long conversion
            return (T) BigInteger.valueOf(number.longValue());
        }
    } else if (Float.class == targetClass) {
        return (T) new Float(number.floatValue());
    } else if (Double.class == targetClass) {
        return (T) new Double(number.doubleValue());
    } else if (BigDecimal.class == targetClass) {
        // always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return (T) new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

From source file:com.projity.server.data.mspdi.ModifiedMSPDIWriter.java

protected void writeProjectCalendar(Project project) {
    // projity addition
    int id = 1; // if not found below, use the default standard calendar
    ProjectCalendar cal = ImportedCalendarService.getInstance().findExportedCalendar(
            CalendarService.findBaseCalendar(m_projectFile.getProjectHeader().getCalendarName()));
    if (cal != null) {
        id = cal.getUniqueID();/*  w ww  . java  2s .  c  o  m*/
    } else {
        log.warn("EXPORT: Could not export project calendar: Project: "
                + m_projectFile.getProjectHeader().getName() + " calendar "
                + m_projectFile.getProjectHeader().getCalendarName());
    }
    project.setCalendarUID(BigInteger.valueOf(id));

}

From source file:it.gualtierotesta.gdocx.GP.java

/**
 * Set font size/*from   w  ww .  jav  a 2  s.c  o m*/
 *
 * @param lFontSize size of the font
 * @return same GP instance
 */
@Nonnull
public GP fontSize(final long lFontSize) {

    Validate.isTrue(0L < lFontSize, "Font size not valid");

    final HpsMeasure hpsMeasure = FACTORY.createHpsMeasure();
    hpsMeasure.setVal(BigInteger.valueOf(lFontSize * 2L));
    getRPr().setSz(hpsMeasure);
    getRPr().setSzCs(hpsMeasure);
    return this;
}

From source file:de.jfachwert.math.Bruch.java

/**
 * Legt einen Bruch mit dem angegeben Zaehler und Nenner an.
 *
 * @param zaehler Zaehler/*from   www . j a va 2  s.com*/
 * @param nenner Nenner
 */
public Bruch(long zaehler, long nenner) {
    this(BigInteger.valueOf(zaehler), BigInteger.valueOf(nenner));
}