List of usage examples for com.google.common.base Preconditions checkArgument
public static void checkArgument(boolean expression)
From source file:org.thinkfree.axihome.NFC.record.TextRecord.java
public static TextRecord parse(NdefRecord record) { Preconditions.checkArgument(record.getTnf() == NdefRecord.TNF_WELL_KNOWN); Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)); try {/*from w w w .j a v a 2 s. c om*/ byte[] payload = record.getPayload(); /* * payload[0] contains the "Status Byte Encodings" field, per the * NFC Forum "Text Record Type Definition" section 3.2.1. * * bit7 is the Text Encoding Field. * * if (Bit_7 == 0): The text is encoded in UTF-8 if (Bit_7 == 1): * The text is encoded in UTF16 * * Bit_6 is reserved for future use and must be set to zero. * * Bits 5 to 0 are the length of the IANA language code. */ String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16"; int languageCodeLength = payload[0] & 0077; String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII"); String text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); return new TextRecord(languageCode, text); } catch (UnsupportedEncodingException e) { // should never happen unless we get a malformed tag. throw new IllegalArgumentException(e); } }
From source file:org.b1.pack.standard.common.Numbers.java
public static byte[] serializeLong(Long value, int size) { try {/*w ww .j a v a 2s .co m*/ ByteArrayOutputStream stream = new ByteArrayOutputStream(); writeLong(value, stream); int minSize = stream.size(); if (minSize == size) { return stream.toByteArray(); } Preconditions.checkArgument(minSize < size); while (stream.size() < size - 1) { stream.write(0x80); } stream.write(0); byte[] result = stream.toByteArray(); result[minSize - 1] |= 0x80; return result; } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.google.gplus.serializer.util.GPlusEventClassifier.java
public static Class detectClass(String json) { Preconditions.checkNotNull(json);// w w w. j ava2 s . c om Preconditions.checkArgument(StringUtils.isNotEmpty(json)); ObjectNode objectNode; try { objectNode = (ObjectNode) mapper.readTree(json); } catch (IOException e) { e.printStackTrace(); return null; } if (objectNode.findValue("kind") != null && objectNode.get("kind").toString().equals(ACTIVITY_IDENTIFIER)) { return Activity.class; } else if (objectNode.findValue("kind") != null && objectNode.get("kind").toString().equals(PERSON_IDENTIFIER)) { return Person.class; } else { return ObjectNode.class; } }
From source file:org.cinchapi.concourse.util.TLists.java
/** * Modify each list to retain only the elements that are contained in all of * the lists./*from w ww. j a v a2 s . c om*/ * * @param lists */ @SafeVarargs public static void retainIntersection(List<?>... lists) { Preconditions.checkArgument(lists.length > 0); List<?> intersection = lists[0]; for (int i = 1; i < lists.length; i++) { intersection.retainAll(lists[i]); } for (int i = 1; i < lists.length; i++) { lists[i].retainAll(intersection); } }
From source file:com.anhth12.lambda.ml.param.DiscreteAround.java
public DiscreteAround(int around, int step) { Preconditions.checkArgument(step > 0); this.around = around; this.step = step; }
From source file:com.cloudera.oryx.kmeans.common.ClusterValidityStatistics.java
public static ClusterValidityStatistics create(Iterable<WeightedRealVector> points, Centers test, Centers train, int replicaId) { Preconditions.checkArgument(test.size() == train.size()); int k = test.size(); double n = 0.0; double testCost = 0.0; double trainCost = 0.0; RealMatrix contingencyMatrix = new Array2DRowRealMatrix(k, k); double[] rowSums = new double[k]; double[] colSums = new double[k]; for (WeightedRealVector wv : points) { Distance d1 = test.getDistance(wv.thing()); Distance d2 = train.getDistance(wv.thing()); contingencyMatrix.addToEntry(d1.getClosestCenterId(), d2.getClosestCenterId(), wv.weight()); rowSums[d1.getClosestCenterId()] += wv.weight(); colSums[d2.getClosestCenterId()] += wv.weight(); testCost += d1.getSquaredDistance() * wv.weight(); trainCost += d2.getSquaredDistance() * wv.weight(); n += wv.weight();//from w ww . j a v a 2 s . c om } return new ClusterValidityStatistics(k, replicaId, testCost, trainCost, normVarInformation(contingencyMatrix, rowSums, colSums, n), normVanDongen(contingencyMatrix, rowSums, colSums, n)); }
From source file:com.anhth12.lambda.ml.param.ContinuousAround.java
ContinuousAround(double around, double step) { Preconditions.checkArgument(step > 0.0); this.around = around; this.step = step; }
From source file:org.apache.streams.facebook.provider.FacebookEventClassifier.java
/** * detectClass from json string.//from w w w . j a v a 2 s . c o m * @param json json string * @return detected Class */ public static Class detectClass(String json) { Objects.requireNonNull(json); Preconditions.checkArgument(StringUtils.isNotEmpty(json)); ObjectNode objectNode; try { objectNode = (ObjectNode) StreamsJacksonMapper.getInstance().readTree(json); } catch (IOException ex) { LOGGER.error("Exception while trying to detect class: {}", ex.getMessage()); return null; } if (objectNode.findValue("about") != null) { return Page.class; } else if (objectNode.findValue("statusType") != null) { return Post.class; } else { return Post.class; } }
From source file:eu.interedition.web.io.RangeConverter.java
private static long toLong(String str) { final long value = Long.valueOf(str); Preconditions.checkArgument(value >= 0); return value; }
From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.NullTestingComparisonAdapter.java
public NullTestingComparisonAdapter(TypeWidget nullableTarget, TypeWidget target) { Preconditions.checkArgument(nullableTarget.isNullable()); Preconditions.checkArgument(!target.isNullable()); this.nullableTarget = nullableTarget; this.target = target.getComparisionAdapter(); }