List of usage examples for java.math BigInteger valueOf
private static BigInteger valueOf(int val[])
From source file:com.web.vehiclerouting.optaplanner.common.persistence.AbstractSolutionImporter.java
public static String getFlooredPossibleSolutionSize(BigInteger possibleSolutionSize) { if (possibleSolutionSize.compareTo(BigInteger.valueOf(1000L)) < 0) { return possibleSolutionSize.toString(); }/* w ww .j ava 2 s . c om*/ // TODO this is slow for machinereassingment's biggest dataset return "10^" + (possibleSolutionSize.toString().length() - 1); }
From source file:com.netflix.imfutility.ConversionHelperTest.java
@Test public void toNewEditRate() { assertEquals(100, ConversionHelper.toNewEditRate(BigInteger.valueOf(160160), new BigFraction(48000), new BigFraction(30000, 1001))); assertEquals(50,// w ww. ja v a 2 s. c o m ConversionHelper.toNewEditRate(BigInteger.valueOf(100), new BigFraction(50), new BigFraction(25))); assertEquals(55, ConversionHelper.toNewEditRate(BigInteger.valueOf(110), new BigFraction(50), new BigFraction(25))); }
From source file:nl.npcf.eav.TestStore.java
@Test public void queries() throws EAVException { EAVStore.Entity entityByKey = eavStore.findByKey(keys.get(15)); Assert.assertEquals(5, ((EAVEntity) entityByKey).getValues().size()); Assert.assertEquals(BigInteger.valueOf(20L), entityByKey.getValues(new EAVPath("age"), false).get(0).getValue()); EAVStore.Attribute ageAttribute = eavStore.getSchema().getAttribute(new EAVPath("age"), true); List<EAVStore.Value> valuesByAge = eavStore.findByAttribute(ageAttribute); Assert.assertEquals(8, valuesByAge.size()); for (EAVStore.Value value : valuesByAge) { Assert.assertEquals("age", value.getAttribute().getPath().getNodeString()); }// w ww .j av a2 s . c om EAVStore.Attribute nameAttribute = eavStore.getSchema().getAttribute(new EAVPath("name"), true); List<EAVStore.Value> valuesByName = eavStore.findByValue(nameAttribute, "Lacey-15"); Assert.assertEquals(1, valuesByName.size()); EAVStore.Value valueByName = valuesByName.get(0); Assert.assertEquals("name", valueByName.getAttribute().getPath().getNodeString()); Assert.assertEquals("Lacey-15", valueByName.getValue()); Assert.assertEquals(5, ((EAVEntity) valueByName.getEntity()).getValues().size()); }
From source file:TSAClient.java
/** * * @param messageImprint imprint of message contents * @return the encoded time stamp token//from w w w. j a v a2 s .c om * @throws IOException if there was an error with the connection or data from the TSA server, * or if the time stamp response could not be validated */ public byte[] getTimeStampToken(byte[] messageImprint) throws IOException { digest.reset(); byte[] hash = digest.digest(messageImprint); // 32-bit cryptographic nonce SecureRandom random = new SecureRandom(); int nonce = random.nextInt(); // generate TSA request TimeStampRequestGenerator tsaGenerator = new TimeStampRequestGenerator(); tsaGenerator.setCertReq(true); ASN1ObjectIdentifier oid = getHashObjectIdentifier(digest.getAlgorithm()); TimeStampRequest request = tsaGenerator.generate(oid, hash, BigInteger.valueOf(nonce)); // get TSA response byte[] tsaResponse = getTSAResponse(request.getEncoded()); TimeStampResponse response; try { response = new TimeStampResponse(tsaResponse); response.validate(request); } catch (TSPException e) { throw new IOException(e); } TimeStampToken token = response.getTimeStampToken(); if (token == null) { throw new IOException("Response does not have a time stamp token"); } return token.getEncoded(); }
From source file:Main.java
/** * convert value to given type.//from ww w. java 2 s .co m * null safe. * * @param value value for convert * @param type will converted type * @return value while converted */ public static Object convertCompatibleType(Object value, Class<?> type) { if (value == null || type == null || type.isAssignableFrom(value.getClass())) { return value; } if (value instanceof String) { String string = (String) value; if (char.class.equals(type) || Character.class.equals(type)) { if (string.length() != 1) { throw new IllegalArgumentException(String.format("CAN NOT convert String(%s) to char!" + " when convert String to char, the String MUST only 1 char.", string)); } return string.charAt(0); } else if (type.isEnum()) { return Enum.valueOf((Class<Enum>) type, string); } else if (type == BigInteger.class) { return new BigInteger(string); } else if (type == BigDecimal.class) { return new BigDecimal(string); } else if (type == Short.class || type == short.class) { return Short.valueOf(string); } else if (type == Integer.class || type == int.class) { return Integer.valueOf(string); } else if (type == Long.class || type == long.class) { return Long.valueOf(string); } else if (type == Double.class || type == double.class) { return Double.valueOf(string); } else if (type == Float.class || type == float.class) { return Float.valueOf(string); } else if (type == Byte.class || type == byte.class) { return Byte.valueOf(string); } else if (type == Boolean.class || type == boolean.class) { return Boolean.valueOf(string); } else if (type == Date.class) { try { return new SimpleDateFormat(DATE_FORMAT).parse((String) value); } catch (ParseException e) { throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT + ", cause: " + e.getMessage(), e); } } else if (type == Class.class) { return forName((String) value); } } else if (value instanceof Number) { Number number = (Number) value; if (type == byte.class || type == Byte.class) { return number.byteValue(); } else if (type == short.class || type == Short.class) { return number.shortValue(); } else if (type == int.class || type == Integer.class) { return number.intValue(); } else if (type == long.class || type == Long.class) { return number.longValue(); } else if (type == float.class || type == Float.class) { return number.floatValue(); } else if (type == double.class || type == Double.class) { return number.doubleValue(); } else if (type == BigInteger.class) { return BigInteger.valueOf(number.longValue()); } else if (type == BigDecimal.class) { return BigDecimal.valueOf(number.doubleValue()); } else if (type == Date.class) { return new Date(number.longValue()); } } else if (value instanceof Collection) { Collection collection = (Collection) value; if (type.isArray()) { int length = collection.size(); Object array = Array.newInstance(type.getComponentType(), length); int i = 0; for (Object item : collection) { Array.set(array, i++, item); } return array; } else if (!type.isInterface()) { try { Collection result = (Collection) type.newInstance(); result.addAll(collection); return result; } catch (Throwable e) { e.printStackTrace(); } } else if (type == List.class) { return new ArrayList<>(collection); } else if (type == Set.class) { return new HashSet<>(collection); } } else if (value.getClass().isArray() && Collection.class.isAssignableFrom(type)) { Collection collection; if (!type.isInterface()) { try { collection = (Collection) type.newInstance(); } catch (Throwable e) { collection = new ArrayList<>(); } } else if (type == Set.class) { collection = new HashSet<>(); } else { collection = new ArrayList<>(); } int length = Array.getLength(value); for (int i = 0; i < length; i++) { collection.add(Array.get(value, i)); } return collection; } return value; }
From source file:ac.elements.parser.SimpleDBConverter.java
/** * Encodes real long value into a string by offsetting and zero-padding * number up to the specified number of digits. Use this encoding method if * the data range set includes both positive and negative values. * //from w w w . j a va 2 s. c om * com.xerox.amazonws.sdb.DataUtils * * @param number * long to be encoded * @return string representation of the long */ private static String encodeLong(long number) { int maxNumDigits = BigInteger.valueOf(Long.MAX_VALUE).subtract(BigInteger.valueOf(Long.MIN_VALUE)) .toString(RADIX).length(); long offsetValue = Long.MIN_VALUE; BigInteger offsetNumber = BigInteger.valueOf(number).subtract(BigInteger.valueOf(offsetValue)); String longString = offsetNumber.toString(RADIX); int numZeroes = maxNumDigits - longString.length(); StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length()); for (int i = 0; i < numZeroes; i++) { strBuffer.insert(i, '0'); } strBuffer.append(longString); return strBuffer.toString(); }
From source file:de.tudarmstadt.ukp.teaching.uima.nounDecompounding.ranking.TotalFreqAmout.java
/** * Adds all frequency values/*from www . j av a 2s . com*/ * @return * @throws IOException */ public BigInteger countFreq() throws IOException { BigInteger count = BigInteger.valueOf(0); for (FSDirectory dir : this.dirs) { count = count.add(this.countFreq(dir)); } return count; }
From source file:com.github.jrrdev.mantisbtsync.core.jobs.projects.ProjectsVersionsWriterTest.java
@Test public void test() throws Exception { final Operation op = sequenceOf( insertInto("mantis_project_table").columns("id", "name").values(1, "project_1").build(), insertInto("mantis_project_version_table").columns("id", "version", "project_id") .values(1, "old_version_1", 1).build()); lauchOperation(op);// www . ja v a 2 s. c o m projectVersionsWriter.write(buildItems()); final List<ProjectVersionData> results = getJdbcTemplate().query( "SELECT id, version as name, project_id" + " FROM mantis_project_version_table" + " WHERE project_id = 1", new BeanPropertyRowMapper<ProjectVersionData>(ProjectVersionData.class)); assertEquals(2, results.size()); for (final ProjectVersionData item : results) { assertEquals(BigInteger.ONE, item.getProject_id()); if (item.getId() == BigInteger.ONE) { assertEquals("new_version_1", item.getName()); } else { assertEquals(BigInteger.valueOf(2), item.getId()); assertEquals("new_version_2", item.getName()); } } }
From source file:jp.aegif.nemaki.model.AttachmentNode.java
public InputStream getInputStream() { if (rangeOffset == null && rangeLength == null) { return inputStream; } else {/*from w w w. j ava 2s.co m*/ if (rangeLength == null) { rangeLength = BigInteger.valueOf(length); } if (rangeLength.intValue() + rangeOffset.intValue() > length) { rangeLength = BigInteger.valueOf(length - rangeOffset.intValue()); } byte[] bytes = new byte[1024]; try { inputStream.read(bytes); ByteArrayInputStream result = new ByteArrayInputStream(bytes, rangeOffset.intValue(), rangeLength.intValue()); return result; } catch (IOException e) { log.error("[attachment id=" + getId() + "]getInputStream with rangeOffset=" + rangeLength.toString() + " rangeLength=" + rangeLength.toString() + " failed.", e); } } return null; }
From source file:com.github.jrrdev.mantisbtsync.core.jobs.issues.readers.CsvIssuesReaderTest.java
@Test public void test() throws Exception { final List<BugIdBean> list = new ArrayList<BugIdBean>(); csvIssuesReader.open(getStepExecution().getExecutionContext()); BugIdBean item = csvIssuesReader.read(); while (item != null) { list.add(item);/*from w ww .j a v a 2 s . co m*/ item = csvIssuesReader.read(); } assertEquals(2, list.size()); assertEquals(BigInteger.valueOf(17428), list.get(0).getId()); assertEquals(BigInteger.valueOf(14234), list.get(1).getId()); }