List of usage examples for java.lang Short MIN_VALUE
short MIN_VALUE
To view the source code for java.lang Short MIN_VALUE.
Click Source Link
From source file:org.apache.phoenix.end2end.index.IndexExpressionIT.java
private void helpTestUpdatableViewIndex(boolean local) throws Exception { Connection conn = DriverManager.getConnection(getUrl()); try {/*from w w w . j av a 2 s . com*/ String ddl = "CREATE TABLE t (k1 INTEGER NOT NULL, k2 INTEGER NOT NULL, k3 DECIMAL, s1 VARCHAR, s2 VARCHAR CONSTRAINT pk PRIMARY KEY (k1, k2, k3))"; conn.createStatement().execute(ddl); ddl = "CREATE VIEW v AS SELECT * FROM t WHERE k1 = 1"; conn.createStatement().execute(ddl); conn.createStatement().execute("UPSERT INTO v(k2,s1,s2,k3) VALUES(120,'foo0','bar0',50.0)"); conn.createStatement().execute("UPSERT INTO v(k2,s1,s2,k3) VALUES(121,'foo1','bar1',51.0)"); conn.commit(); ResultSet rs; conn.createStatement() .execute("CREATE " + (local ? "LOCAL" : "") + " INDEX i1 on v(k1+k2+k3) include (s1, s2)"); conn.createStatement().execute("UPSERT INTO v(k2,s1,s2,k3) VALUES(120,'foo2','bar2',50.0)"); conn.commit(); String query = "SELECT k1, k2, k3, s1, s2 FROM v WHERE k1+k2+k3 = 173.0"; rs = conn.createStatement().executeQuery("EXPLAIN " + query); String queryPlan = QueryUtil.getExplainPlan(rs); if (local) { assertEquals( "CLIENT PARALLEL 1-WAY RANGE SCAN OVER _LOCAL_IDX_T [-32768,173]\n" + "CLIENT MERGE SORT", queryPlan); } else { assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER _IDX_T [" + Short.MIN_VALUE + ",173]", queryPlan); } rs = conn.createStatement().executeQuery(query); assertTrue(rs.next()); assertEquals(1, rs.getInt(1)); assertEquals(121, rs.getInt(2)); assertTrue(BigDecimal.valueOf(51.0).compareTo(rs.getBigDecimal(3)) == 0); assertEquals("foo1", rs.getString(4)); assertEquals("bar1", rs.getString(5)); assertFalse(rs.next()); conn.createStatement().execute("CREATE " + (local ? "LOCAL" : "") + " INDEX i2 on v(s1||'_'||s2)"); query = "SELECT k1, k2, s1||'_'||s2 FROM v WHERE (s1||'_'||s2)='foo2_bar2'"; rs = conn.createStatement().executeQuery("EXPLAIN " + query); if (local) { assertEquals( "CLIENT PARALLEL 1-WAY RANGE SCAN OVER _LOCAL_IDX_T [" + (Short.MIN_VALUE + 1) + ",'foo2_bar2']\n" + " SERVER FILTER BY FIRST KEY ONLY\n" + "CLIENT MERGE SORT", QueryUtil.getExplainPlan(rs)); } else { assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER _IDX_T [" + (Short.MIN_VALUE + 1) + ",'foo2_bar2']\n" + " SERVER FILTER BY FIRST KEY ONLY", QueryUtil.getExplainPlan(rs)); } rs = conn.createStatement().executeQuery(query); assertTrue(rs.next()); assertEquals(1, rs.getInt(1)); assertEquals(120, rs.getInt(2)); assertEquals("foo2_bar2", rs.getString(3)); assertFalse(rs.next()); } finally { conn.close(); } }
From source file:org.geotools.gce.imagemosaic.RasterLayerResponse.java
/** * This method is responsible for creating a coverage from the supplied {@link RenderedImage}. * //from w w w . j a va 2 s . c o m * @param image * @return * @throws IOException */ private GridCoverage2D prepareCoverage(RenderedImage image) throws IOException { // creating bands final SampleModel sm = image.getSampleModel(); final ColorModel cm = image.getColorModel(); final int numBands = sm.getNumBands(); final GridSampleDimension[] bands = new GridSampleDimension[numBands]; Set<String> bandNames = new HashSet<String>(); // setting bands names. for (int i = 0; i < numBands; i++) { ColorInterpretation colorInterpretation = null; String bandName = null; if (cm != null) { // === color interpretation colorInterpretation = TypeMap.getColorInterpretation(cm, i); if (colorInterpretation == null) { throw new IOException("Unrecognized sample dimension type"); } bandName = colorInterpretation.name(); if (bandNames.contains(bandName)) {// make sure we create no duplicate band names bandName = "Band" + (i + 1); } } else { // no color model bandName = "Band" + (i + 1); colorInterpretation = ColorInterpretation.UNDEFINED; } // sample dimension type final SampleDimensionType st = TypeMap.getSampleDimensionType(sm, i); // set some no data values, as well as Min and Max values final double noData; double min = -Double.MAX_VALUE, max = Double.MAX_VALUE; if (backgroundValues != null) { // sometimes background values are not specified as 1 per each band, therefore we need to be careful noData = backgroundValues[backgroundValues.length > i ? i : 0]; } else { if (st.compareTo(SampleDimensionType.REAL_32BITS) == 0) noData = Float.NaN; else if (st.compareTo(SampleDimensionType.REAL_64BITS) == 0) noData = Double.NaN; else if (st.compareTo(SampleDimensionType.SIGNED_16BITS) == 0) { noData = Short.MIN_VALUE; min = Short.MIN_VALUE; max = Short.MAX_VALUE; } else if (st.compareTo(SampleDimensionType.SIGNED_32BITS) == 0) { noData = Integer.MIN_VALUE; min = Integer.MIN_VALUE; max = Integer.MAX_VALUE; } else if (st.compareTo(SampleDimensionType.SIGNED_8BITS) == 0) { noData = -128; min = -128; max = 127; } else { //unsigned noData = 0; min = 0; // compute max if (st.compareTo(SampleDimensionType.UNSIGNED_1BIT) == 0) max = 1; else if (st.compareTo(SampleDimensionType.UNSIGNED_2BITS) == 0) max = 3; else if (st.compareTo(SampleDimensionType.UNSIGNED_4BITS) == 0) max = 7; else if (st.compareTo(SampleDimensionType.UNSIGNED_8BITS) == 0) max = 255; else if (st.compareTo(SampleDimensionType.UNSIGNED_16BITS) == 0) max = 65535; else if (st.compareTo(SampleDimensionType.UNSIGNED_32BITS) == 0) max = Math.pow(2, 32) - 1; } } bands[i] = new SimplifiedGridSampleDimension(bandName, st, colorInterpretation, noData, min, max, 1, //no scale 0, //no offset null).geophysics(true); } // creating the final coverage by keeping into account the fact that we Map<String, String> properties = null; if (granulesPaths != null) { properties = new HashMap<String, String>(); properties.put(AbstractGridCoverage2DReader.FILE_SOURCE_PROPERTY, granulesPaths); } return coverageFactory.create(rasterManager.getCoverageIdentifier(), image, new GridGeometry2D(new GridEnvelope2D(PlanarImage.wrapRenderedImage(image).getBounds()), PixelInCell.CELL_CORNER, finalGridToWorldCorner, this.mosaicBBox.getCoordinateReferenceSystem(), hints), bands, null, properties); }
From source file:org.apache.geode.internal.InternalDataSerializer.java
public static void writeDSFIDHeader(int dsfid, DataOutput out) throws IOException { if (dsfid == DataSerializableFixedID.ILLEGAL) { throw new IllegalStateException( LocalizedStrings.InternalDataSerializer_ATTEMPTED_TO_SERIALIZE_ILLEGAL_DSFID .toLocalizedString()); }/* w w w . ja v a 2s. c om*/ if (dsfid <= Byte.MAX_VALUE && dsfid >= Byte.MIN_VALUE) { out.writeByte(DS_FIXED_ID_BYTE); out.writeByte(dsfid); } else if (dsfid <= Short.MAX_VALUE && dsfid >= Short.MIN_VALUE) { out.writeByte(DS_FIXED_ID_SHORT); out.writeShort(dsfid); } else { out.writeByte(DS_FIXED_ID_INT); out.writeInt(dsfid); } }
From source file:org.apache.geode.internal.InternalDataSerializer.java
/** * Data serializes an instance of a "user class" (that is, a class that can be handled by a * registered {@code DataSerializer}) to the given {@code DataOutput}. * * @return {@code true} if {@code o} was written to {@code out}. *//*from w w w.j av a2 s . c o m*/ private static boolean writeUserObject(Object o, DataOutput out, boolean ensurePdxCompatibility) throws IOException { final Class<?> c = o.getClass(); final DataSerializer serializer = InternalDataSerializer.getSerializer(c); if (serializer != null) { int id = serializer.getId(); if (id != 0) { checkPdxCompatible(o, ensurePdxCompatibility); // id will be 0 if it is a WellKnowDS if (id <= Byte.MAX_VALUE && id >= Byte.MIN_VALUE) { out.writeByte(USER_CLASS); out.writeByte((byte) id); } else if (id <= Short.MAX_VALUE && id >= Short.MIN_VALUE) { out.writeByte(USER_CLASS_2); out.writeShort(id); } else { out.writeByte(USER_CLASS_4); out.writeInt(id); } } else { if (ensurePdxCompatibility) { if (!(serializer instanceof WellKnownPdxDS)) { checkPdxCompatible(o, ensurePdxCompatibility); } } } boolean toDataResult; try { toDataResult = serializer.toData(o, out); } catch (IOException io) { if (serializer instanceof WellKnownDS) { // this is not user code so throw IOException throw io; // see bug 44659 } else { // We no longer rethrow IOException here // because if user code throws an IOException we want // to create a ToDataException to report it as a problem // with the plugin code. throw new ToDataException("toData failed on DataSerializer with id=" + id + " for class " + c, io); } } catch (CancelException | ToDataException | GemFireRethrowable ex) { // Serializing a PDX can result in a cache closed exception. Just rethrow throw ex; } catch (VirtualMachineError err) { SystemFailure.initiateFailure(err); // If this ever returns, rethrow the error. We're poisoned // now, so don't let this thread continue. throw err; } catch (Throwable t) { // Whenever you catch Error or Throwable, you must also // catch VirtualMachineError (see above). However, there is // _still_ a possibility that you are dealing with a cascading // error condition, so you also need to check to see if the JVM // is still usable: SystemFailure.checkFailure(); throw new ToDataException("toData failed on DataSerializer with id=" + id + " for class " + c, t); } if (toDataResult) { return true; } else { throw new ToDataException( LocalizedStrings.DataSerializer_SERIALIZER_0_A_1_SAID_THAT_IT_COULD_SERIALIZE_AN_INSTANCE_OF_2_BUT_ITS_TODATA_METHOD_RETURNED_FALSE .toLocalizedString(serializer.getId(), serializer.getClass().getName(), o.getClass().getName())); } // Do byte[][] and Object[] here to fix bug 44060 } else if (o instanceof byte[][]) { byte[][] byteArrays = (byte[][]) o; out.writeByte(ARRAY_OF_BYTE_ARRAYS); writeArrayOfByteArrays(byteArrays, out); return true; } else if (o instanceof Object[]) { Object[] array = (Object[]) o; out.writeByte(OBJECT_ARRAY); writeObjectArray(array, out, ensurePdxCompatibility); return true; } else if (is662SerializationEnabled() && (o.getClass().isEnum()/* for bug 52271 */ || (o.getClass().getSuperclass() != null && o.getClass().getSuperclass().isEnum()))) { if (isPdxSerializationInProgress()) { writePdxEnum((Enum<?>) o, out); } else { checkPdxCompatible(o, ensurePdxCompatibility); writeGemFireEnum((Enum<?>) o, out); } return true; } else { PdxSerializer pdxSerializer = TypeRegistry.getPdxSerializer(); return pdxSerializer != null && writePdx(out, null, o, pdxSerializer); } }
From source file:org.apache.geode.internal.InternalDataSerializer.java
public static void writeUserDataSerializableHeader(int classId, DataOutput out) throws IOException { if (classId <= Byte.MAX_VALUE && classId >= Byte.MIN_VALUE) { out.writeByte(USER_DATA_SERIALIZABLE); out.writeByte(classId);/*w w w . jav a 2 s. c o m*/ } else if (classId <= Short.MAX_VALUE && classId >= Short.MIN_VALUE) { out.writeByte(USER_DATA_SERIALIZABLE_2); out.writeShort(classId); } else { out.writeByte(USER_DATA_SERIALIZABLE_4); out.writeInt(classId); } }
From source file:org.apache.rya.indexing.smarturi.duplication.DuplicateDataDetectorIT.java
@Test public void testShortProperty() throws SmartUriException { System.out.println("Short Property Test"); final ImmutableList.Builder<TestInput> builder = ImmutableList.builder(); // Tolerance 0.0 Tolerance tolerance = new Tolerance(0.0, ToleranceType.DIFFERENCE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, false)); builder.add(new TestInput((short) -1, tolerance, false)); builder.add(new TestInput((short) 0, tolerance, false)); builder.add(new TestInput((short) 1, tolerance, false)); builder.add(new TestInput((short) 37, tolerance, false)); builder.add(new TestInput((short) 38, tolerance, false)); builder.add(new TestInput((short) 39, tolerance, false)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, false)); builder.add(new TestInput((short) 42, tolerance, false)); builder.add(new TestInput((short) 43, tolerance, false)); builder.add(new TestInput((short) 100, tolerance, false)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, false)); // Tolerance 1.0 tolerance = new Tolerance(1.0, ToleranceType.DIFFERENCE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, false)); builder.add(new TestInput((short) -1, tolerance, false)); builder.add(new TestInput((short) 0, tolerance, false)); builder.add(new TestInput((short) 1, tolerance, false)); builder.add(new TestInput((short) 37, tolerance, false)); builder.add(new TestInput((short) 38, tolerance, false)); builder.add(new TestInput((short) 39, tolerance, true)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, true)); builder.add(new TestInput((short) 42, tolerance, false)); builder.add(new TestInput((short) 43, tolerance, false)); builder.add(new TestInput((short) 100, tolerance, false)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, false)); // Tolerance 2.0 tolerance = new Tolerance(2.0, ToleranceType.DIFFERENCE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, false)); builder.add(new TestInput((short) -1, tolerance, false)); builder.add(new TestInput((short) 0, tolerance, false)); builder.add(new TestInput((short) 1, tolerance, false)); builder.add(new TestInput((short) 37, tolerance, false)); builder.add(new TestInput((short) 38, tolerance, true)); builder.add(new TestInput((short) 39, tolerance, true)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, true)); builder.add(new TestInput((short) 42, tolerance, true)); builder.add(new TestInput((short) 43, tolerance, false)); builder.add(new TestInput((short) 100, tolerance, false)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, false)); // Tolerance 0.0% tolerance = new Tolerance(0.00, ToleranceType.PERCENTAGE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, false)); builder.add(new TestInput((short) -1, tolerance, false)); builder.add(new TestInput((short) 0, tolerance, false)); builder.add(new TestInput((short) 1, tolerance, false)); builder.add(new TestInput((short) 37, tolerance, false)); builder.add(new TestInput((short) 38, tolerance, false)); builder.add(new TestInput((short) 39, tolerance, false)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, false)); builder.add(new TestInput((short) 42, tolerance, false)); builder.add(new TestInput((short) 43, tolerance, false)); builder.add(new TestInput((short) 100, tolerance, false)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, false)); // Tolerance 10.0% tolerance = new Tolerance(0.10, ToleranceType.PERCENTAGE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, false)); builder.add(new TestInput((short) -1, tolerance, false)); builder.add(new TestInput((short) 0, tolerance, false)); builder.add(new TestInput((short) 1, tolerance, false)); builder.add(new TestInput((short) 35, tolerance, false)); builder.add(new TestInput((short) 36, tolerance, true)); builder.add(new TestInput((short) 37, tolerance, true)); builder.add(new TestInput((short) 38, tolerance, true)); builder.add(new TestInput((short) 39, tolerance, true)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, true)); builder.add(new TestInput((short) 42, tolerance, true)); builder.add(new TestInput((short) 43, tolerance, true)); builder.add(new TestInput((short) 44, tolerance, true)); builder.add(new TestInput((short) 45, tolerance, false)); builder.add(new TestInput((short) 100, tolerance, false)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, false)); // Tolerance 100.0% tolerance = new Tolerance(1.00, ToleranceType.PERCENTAGE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, true)); builder.add(new TestInput((short) -1, tolerance, true)); builder.add(new TestInput((short) 0, tolerance, true)); builder.add(new TestInput((short) 1, tolerance, true)); builder.add(new TestInput((short) 35, tolerance, true)); builder.add(new TestInput((short) 36, tolerance, true)); builder.add(new TestInput((short) 37, tolerance, true)); builder.add(new TestInput((short) 38, tolerance, true)); builder.add(new TestInput((short) 39, tolerance, true)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, true)); builder.add(new TestInput((short) 42, tolerance, true)); builder.add(new TestInput((short) 43, tolerance, true)); builder.add(new TestInput((short) 44, tolerance, true)); builder.add(new TestInput((short) 45, tolerance, true)); builder.add(new TestInput((short) 100, tolerance, true)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, true)); final ImmutableList<TestInput> testInputs = builder.build(); testProperty(testInputs, PERSON_TYPE_URI, HAS_AGE); }
From source file:org.apache.rya.indexing.smarturi.duplication.DuplicateDataDetectorTest.java
@Test public void testShortProperty() throws SmartUriException { System.out.println("Short Property Test"); final ImmutableList.Builder<TestInput> builder = ImmutableList.<TestInput>builder(); // Tolerance 0.0 Tolerance tolerance = new Tolerance(0.0, ToleranceType.DIFFERENCE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, false)); builder.add(new TestInput((short) -1, tolerance, false)); builder.add(new TestInput((short) 0, tolerance, false)); builder.add(new TestInput((short) 1, tolerance, false)); builder.add(new TestInput((short) 37, tolerance, false)); builder.add(new TestInput((short) 38, tolerance, false)); builder.add(new TestInput((short) 39, tolerance, false)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, false)); builder.add(new TestInput((short) 42, tolerance, false)); builder.add(new TestInput((short) 43, tolerance, false)); builder.add(new TestInput((short) 100, tolerance, false)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, false)); // Tolerance 1.0 tolerance = new Tolerance(1.0, ToleranceType.DIFFERENCE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, false)); builder.add(new TestInput((short) -1, tolerance, false)); builder.add(new TestInput((short) 0, tolerance, false)); builder.add(new TestInput((short) 1, tolerance, false)); builder.add(new TestInput((short) 37, tolerance, false)); builder.add(new TestInput((short) 38, tolerance, false)); builder.add(new TestInput((short) 39, tolerance, true)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, true)); builder.add(new TestInput((short) 42, tolerance, false)); builder.add(new TestInput((short) 43, tolerance, false)); builder.add(new TestInput((short) 100, tolerance, false)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, false)); // Tolerance 2.0 tolerance = new Tolerance(2.0, ToleranceType.DIFFERENCE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, false)); builder.add(new TestInput((short) -1, tolerance, false)); builder.add(new TestInput((short) 0, tolerance, false)); builder.add(new TestInput((short) 1, tolerance, false)); builder.add(new TestInput((short) 37, tolerance, false)); builder.add(new TestInput((short) 38, tolerance, true)); builder.add(new TestInput((short) 39, tolerance, true)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, true)); builder.add(new TestInput((short) 42, tolerance, true)); builder.add(new TestInput((short) 43, tolerance, false)); builder.add(new TestInput((short) 100, tolerance, false)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, false)); // Tolerance 0.0% tolerance = new Tolerance(0.00, ToleranceType.PERCENTAGE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, false)); builder.add(new TestInput((short) -1, tolerance, false)); builder.add(new TestInput((short) 0, tolerance, false)); builder.add(new TestInput((short) 1, tolerance, false)); builder.add(new TestInput((short) 37, tolerance, false)); builder.add(new TestInput((short) 38, tolerance, false)); builder.add(new TestInput((short) 39, tolerance, false)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, false)); builder.add(new TestInput((short) 42, tolerance, false)); builder.add(new TestInput((short) 43, tolerance, false)); builder.add(new TestInput((short) 100, tolerance, false)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, false)); // Tolerance 10.0% tolerance = new Tolerance(0.10, ToleranceType.PERCENTAGE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, false)); builder.add(new TestInput((short) -1, tolerance, false)); builder.add(new TestInput((short) 0, tolerance, false)); builder.add(new TestInput((short) 1, tolerance, false)); builder.add(new TestInput((short) 35, tolerance, false)); builder.add(new TestInput((short) 36, tolerance, true)); builder.add(new TestInput((short) 37, tolerance, true)); builder.add(new TestInput((short) 38, tolerance, true)); builder.add(new TestInput((short) 39, tolerance, true)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, true)); builder.add(new TestInput((short) 42, tolerance, true)); builder.add(new TestInput((short) 43, tolerance, true)); builder.add(new TestInput((short) 44, tolerance, true)); builder.add(new TestInput((short) 45, tolerance, false)); builder.add(new TestInput((short) 100, tolerance, false)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, false)); // Tolerance 100.0% tolerance = new Tolerance(1.00, ToleranceType.PERCENTAGE); builder.add(new TestInput(Short.MIN_VALUE, tolerance, true)); builder.add(new TestInput((short) -1, tolerance, true)); builder.add(new TestInput((short) 0, tolerance, true)); builder.add(new TestInput((short) 1, tolerance, true)); builder.add(new TestInput((short) 35, tolerance, true)); builder.add(new TestInput((short) 36, tolerance, true)); builder.add(new TestInput((short) 37, tolerance, true)); builder.add(new TestInput((short) 38, tolerance, true)); builder.add(new TestInput((short) 39, tolerance, true)); builder.add(new TestInput((short) 40, tolerance, true)); // Equals value builder.add(new TestInput((short) 41, tolerance, true)); builder.add(new TestInput((short) 42, tolerance, true)); builder.add(new TestInput((short) 43, tolerance, true)); builder.add(new TestInput((short) 44, tolerance, true)); builder.add(new TestInput((short) 45, tolerance, true)); builder.add(new TestInput((short) 100, tolerance, true)); builder.add(new TestInput(Short.MAX_VALUE, tolerance, true)); final ImmutableList<TestInput> testInputs = builder.build(); testProperty(testInputs, PERSON_TYPE_URI, HAS_AGE); }
From source file:com.datatorrent.lib.appdata.gpo.GPOUtils.java
/** * Determines if the given value is within the range of the specified type. * @param type The type to determine the range of. Valid types can be byte or short. * @param val The value to check the range of. * @return True if the given int value is within the range of the specified type, false otherwise. *//*from w w w . j ava 2 s . c o m*/ public static boolean insideRange(Type type, int val) { switch (type) { case BYTE: { return !(val < (int) Byte.MIN_VALUE || val > (int) Byte.MAX_VALUE); } case SHORT: { return !(val < (int) Short.MIN_VALUE || val > (int) Short.MAX_VALUE); } default: throw new UnsupportedOperationException("This operation is not supported for the type " + type); } }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils.NumberObject} */// ww w .j av a 2 s . co m @SuppressWarnings("unchecked") @Test public void NumberObjectTest() { try { Class<?> numberObject = ClassLoader.getSystemClassLoader() .loadClass(NumberUtils.class.getName() + "$NumberObject"); Constructor<?> c = numberObject.getDeclaredConstructor(Class.class); c.setAccessible(true); Method ofType = numberObject.getMethod("of", Class.class); ofType.setAccessible(true); Method ofN = numberObject.getMethod("of", Number.class); ofN.setAccessible(true); Method parsable = numberObject.getDeclaredMethod("parsable", Number.class); parsable.setAccessible(true); Method contains = numberObject.getDeclaredMethod("contains", Number.class); contains.setAccessible(true); Method valueOf = numberObject.getDeclaredMethod("valueOf", Number.class); valueOf.setAccessible(true); for (Class<?> type : NUMBERS) { Object o = c.newInstance(type); Class<? extends Number> wrapper = (Class<? extends Number>) ClassUtils.primitiveToWrapper(type); Object numob = ofType.invoke(null, type); assertEquals("ofType: " + type.getSimpleName(), o, numob); Number n = null; if (!type.isPrimitive()) { if (ClassUtils.isPrimitiveWrapper(type)) { n = (Number) ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class) .invoke(null, "1"); } else { n = (Number) type.getField("ONE").get(null); } if (type.equals(byte.class)) assertEquals("ofN: 1: " + type.getSimpleName(), o, ofN.invoke(null, n)); } assertEquals("parsable: -1: " + type.getSimpleName(), true, parsable.invoke(numob, -1)); assertEquals("parsable: 0: " + type.getSimpleName(), true, parsable.invoke(numob, 0)); assertEquals("parsable: 1: " + type.getSimpleName(), true, parsable.invoke(numob, 1)); assertEquals("parsable: null: " + type.getSimpleName(), !type.isPrimitive(), parsable.invoke(numob, (Number) null)); Object expected = ObjectUtils.isAny(wrapper, Float.class, Double.class, BigDecimal.class, BigInteger.class); assertEquals("parsable: Infinity: Double: " + type.getSimpleName(), expected, parsable.invoke(numob, Double.POSITIVE_INFINITY)); assertEquals("parsable: Infinity: Double: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE)); assertEquals("parsable: Infinity: Double: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE.toBigInteger())); assertEquals("parsable: Infinity: Float: " + type.getSimpleName(), expected, parsable.invoke(numob, Float.POSITIVE_INFINITY)); assertEquals("parsable: Infinity: Float: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT)); assertEquals("parsable: Infinity: Float: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT.toBigInteger())); assertEquals("parsable: -Infinity: Double: " + type.getSimpleName(), expected, parsable.invoke(numob, Double.NEGATIVE_INFINITY)); assertEquals("parsable: -Infinity: Double: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE.negate())); assertEquals("parsable: -Infinity: Double: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE.negate().toBigInteger())); assertEquals("parsable: -Infinity: Float: " + type.getSimpleName(), expected, parsable.invoke(numob, Float.NEGATIVE_INFINITY)); assertEquals("parsable: -Infinity: Float: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT.negate())); assertEquals("parsable: -Infinity: Float: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT.negate().toBigInteger())); expected = ObjectUtils.isAny(wrapper, Float.class, Double.class); assertEquals("parsable: NaN: Float: " + type.getSimpleName(), expected, parsable.invoke(numob, Float.NaN)); assertEquals("parsable: NaN: Double: " + type.getSimpleName(), expected, parsable.invoke(numob, Double.NaN)); if (Byte.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getByte(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getByte(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, Short.MIN_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Short.MAX_VALUE)); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123.456f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getByte(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getByte(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, Short.MIN_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Short.MAX_VALUE)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123.456f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 1234.56f)); } if (Short.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getShort(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getShort(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, Integer.MIN_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Integer.MAX_VALUE)); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123.456f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getShort(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getShort(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, Integer.MIN_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Integer.MAX_VALUE)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 12345.6f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 123456.789f)); } if (Integer.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getInt(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getInt(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, Long.MIN_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Long.MAX_VALUE)); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123456.789f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getInt(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getInt(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, Long.MIN_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Long.MAX_VALUE)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123456.789f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 12345678912345678912.3456d)); } if (Long.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getLong(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getLong(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, BigInteger.valueOf(Long.MIN_VALUE).pow(2))); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, BigInteger.valueOf(Long.MAX_VALUE).pow(2))); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123.456f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getLong(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getLong(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, BigInteger.valueOf(Long.MIN_VALUE).pow(2))); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, BigInteger.valueOf(Long.MAX_VALUE).pow(2))); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123456.789f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 12345678912345678912.3456f)); } if (Float.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, -wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, -Double.MAX_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Double.MAX_VALUE)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, -wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, -Double.MAX_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Double.MAX_VALUE)); } if (Double.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, -wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), true, parsable.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN).negate())); assertEquals("parsable: overflow: max: " + type.getSimpleName(), true, parsable.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN))); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, -wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN).negate())); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN))); } if (!ClassUtils.isPrimitiveWrapper(wrapper)) { assertEquals("parsable: fraction: " + type.getSimpleName(), BigDecimal.class.equals(type), parsable.invoke(numob, 123.456f)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123.456f)); } if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "123"); } else { expected = new BigDecimal("123"); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } for (Class<?> valueType : OBJECTS) { if (ClassUtils.isPrimitiveWrapper(valueType)) { n = (Number) valueType.getMethod("valueOf", String.class).invoke(null, "123"); } else { n = new BigDecimal("123"); if (BigInteger.class.equals(valueType)) n = ((BigDecimal) n).toBigInteger(); } assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); } n = 123.456f; if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, n.toString()); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, Integer.toString(((Float) n).intValue())); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); n = 1.23456789E-6d; if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, n.toString()); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, Integer.toString(((Double) n).intValue())); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); n = INFINITY_DOUBLE.pow(2); if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getField("POSITIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MAX_VALUE").get(null); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: Huge: " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals("valueOf: Huge: class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); n = INFINITY_DOUBLE.pow(2).negate(); if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getField("NEGATIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MIN_VALUE").get(null); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: Huge: negative: " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals("valueOf: Huge: negative: class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } }