List of usage examples for java.lang Short Short
@Deprecated(since = "9") public Short(String s) throws NumberFormatException
From source file:MutableShort.java
/** * Gets this mutable as an instance of Short. * * @return a Short instance containing the value from this mutable *//*from w w w. j av a 2 s.c o m*/ public Short toShort() { return new Short(shortValue()); }
From source file:ConversionUtil.java
public static Object convertToValue(Class aClass, byte[] inputArray) throws Exception { Object returnValue = null;//from w ww.ja v a2 s .co m String className = aClass.getName(); if (className.equals(Integer.class.getName())) { returnValue = new Integer(convertToInt(inputArray)); } else if (className.equals(String.class.getName())) { returnValue = convertToString(inputArray); } else if (className.equals(Byte.class.getName())) { returnValue = new Byte(convertToByte(inputArray)); } else if (className.equals(Long.class.getName())) { returnValue = new Long(convertToLong(inputArray)); } else if (className.equals(Short.class.getName())) { returnValue = new Short(convertToShort(inputArray)); } else if (className.equals(Boolean.class.getName())) { returnValue = new Boolean(convertToBoolean(inputArray)); } else { throw new Exception("Cannot convert object of type " + className); } return returnValue; }
From source file:NumberUtils.java
/** * Convert the given number into an instance of the given target class. * @param number the number to convert/*from w w w . j a v a 2 s. c om*/ * @param targetClass the target class to convert to * @return the converted number * @throws IllegalArgumentException if the target class is not supported * (i.e. not a standard Number subclass as included in the JDK) * @see java.lang.Byte * @see java.lang.Short * @see java.lang.Integer * @see java.lang.Long * @see java.math.BigInteger * @see java.lang.Float * @see java.lang.Double * @see java.math.BigDecimal */ public static Number convertNumberToTargetClass(Number number, Class<?> targetClass) throws IllegalArgumentException { // Assert.notNull(number, "Number must not be null"); // Assert.notNull(targetClass, "Target class must not be null"); if (targetClass.isInstance(number)) { return number; } else if (targetClass.equals(Byte.class)) { long value = number.longValue(); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return new Byte(number.byteValue()); } else if (targetClass.equals(Short.class)) { long value = number.longValue(); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return new Short(number.shortValue()); } else if (targetClass.equals(Integer.class)) { long value = number.longValue(); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return new Integer(number.intValue()); } else if (targetClass.equals(Long.class)) { return new Long(number.longValue()); } else if (targetClass.equals(Float.class)) { return new Float(number.floatValue()); } else if (targetClass.equals(Double.class)) { return new Double(number.doubleValue()); } else if (targetClass.equals(BigInteger.class)) { return BigInteger.valueOf(number.longValue()); } else if (targetClass.equals(BigDecimal.class)) { // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double) // (see BigDecimal javadoc for details) return 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.google.api.server.spi.config.annotationreader.ApiAnnotationIntrospectorTest.java
@Test public void testDeserializeCustomResourceSerializerInheritance() throws Exception { String json = "{\"nestedResource\": {}, \"aShort\": 99}"; TestChildResourceWithCustomSerializer value = objectMapper.readValue(json, TestChildResourceWithCustomSerializer.class); assertEquals("0,0", value.point); assertNotNull(value.nested);// ww w . ja v a 2 s . c om assertEquals("test", value.nested.foo); assertEquals(new Short((short) 99), value.number); }
From source file:MathUtils.java
/** * Convert a String in Double, Float, Integer, Long, Short or Byte, depending on the T exemple * For exemple convert("2", 0f) will return 2.0 as a float * @param <T> type returned//from www.ja v a 2s.c om * @param str String to convert * @param exemple exemple of the type * @return a number representation of the String * @throws IllegalArgumentException if the T type is not Double, Float, Integer, Long, Short or Byte * @throws NumberFormatException if the conversion fails */ public static <T extends Number> T convert(String str, T exemple) throws NumberFormatException, IllegalArgumentException { T result = null; if (exemple instanceof Double) { result = (T) new Double(str); } else if (exemple instanceof Float) { result = (T) new Float(str); } else if (exemple instanceof Integer) { result = (T) new Integer(str); } else if (exemple instanceof Long) { result = (T) new Long(str); } else if (exemple instanceof Short) { result = (T) new Short(str); } else if (exemple instanceof Byte) { result = (T) new Byte(str); } else { throw new IllegalArgumentException("Conversion is not possible with class " + exemple.getClass() + "; only allowing Double, Float, integer, Long, Short & Byte"); } return result; }
From source file:com.amazonaws.hal.client.ConversionUtil.java
private static Object convertFromString(Class<?> clazz, String value) { if (String.class.isAssignableFrom(clazz)) { return value; } else if (int.class.isAssignableFrom(clazz) || Integer.class.isAssignableFrom(clazz)) { return new Integer(value); } else if (long.class.isAssignableFrom(clazz) || Long.class.isAssignableFrom(clazz)) { return new Long(value); } else if (short.class.isAssignableFrom(clazz) || Short.class.isAssignableFrom(clazz)) { return new Short(value); } else if (double.class.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz)) { return new Double(value); } else if (float.class.isAssignableFrom(clazz) || Float.class.isAssignableFrom(clazz)) { return new Float(value); } else if (boolean.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz)) { return Boolean.valueOf(value); } else if (char.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz)) { return value.charAt(0); } else if (byte.class.isAssignableFrom(clazz) || Byte.class.isAssignableFrom(clazz)) { return new Byte(value); } else if (BigDecimal.class.isAssignableFrom(clazz)) { return new BigDecimal(value); } else if (BigInteger.class.isAssignableFrom(clazz)) { return new BigInteger(value); } else if (Date.class.isAssignableFrom(clazz)) { try {//from w w w .j a v a 2s . co m return new Date(Long.parseLong(value)); } catch (NumberFormatException e) { try { return DatatypeConverter.parseDateTime(value).getTime(); } catch (IllegalArgumentException e1) { throw new RuntimeException("Unexpected date format: " + value + ". We currently parse xsd:datetime and milliseconds."); } } } else if (clazz.isEnum()) { try { //noinspection unchecked return Enum.valueOf((Class<Enum>) clazz, value); } catch (IllegalArgumentException e) { log.error(String.format( "'%s' is not a recognized enum value for %s. Returning default of %s instead.", value, clazz.getName(), clazz.getEnumConstants()[0])); return clazz.getEnumConstants()[0]; } } else { throw new RuntimeException("Not sure how to convert " + value + " to a " + clazz.getSimpleName()); } }
From source file:com.enonic.esl.containers.ExtendedMap.java
public Object putShort(Object key, short value) { return put(key, new Short(value)); }
From source file:net.sourceforge.msscodefactory.cfcore.v2_0.CFGenKbXMsgRspnHandler.CFGenKbXMsgRspnDefClassRecHandler.java
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { try {// w w w. j av a 2 s. com // Common XML Attributes String attrId = null; String attrRevision = null; // DefClass Attributes String attrName = null; String attrBaseId = null; // Attribute Extraction String attrLocalName; int numAttrs; int idxAttr; final String S_ProcName = "startElement"; final String S_LocalName = "LocalName"; assert qName.equals("DefClass"); CFGenKbXMsgRspnHandler xmsgRspnHandler = (CFGenKbXMsgRspnHandler) getParser(); if (xmsgRspnHandler == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser()"); } ICFGenKbSchemaObj schemaObj = xmsgRspnHandler.getSchemaObj(); if (schemaObj == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser().getSchemaObj()"); } // Extract Attributes numAttrs = attrs.getLength(); for (idxAttr = 0; idxAttr < numAttrs; idxAttr++) { attrLocalName = attrs.getLocalName(idxAttr); if (attrLocalName.equals("Id")) { if (attrId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("Revision")) { if (attrRevision != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrRevision = attrs.getValue(idxAttr); } else if (attrLocalName.equals("Name")) { if (attrName != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrName = attrs.getValue(idxAttr); } else if (attrLocalName.equals("BaseId")) { if (attrBaseId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrBaseId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("schemaLocation")) { // ignored } else { throw CFLib.getDefaultExceptionFactory().newUnrecognizedAttributeException(getClass(), S_ProcName, getParser().getLocationInfo(), attrLocalName); } } // Ensure that required attributes have values if ((attrId == null) || (attrId.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "Id"); } if (attrName == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "Name"); } // Save named attributes to context CFLibXmlCoreContext curContext = xmsgRspnHandler.getCurContext(); // Convert string attributes to native Java types short natId = Short.parseShort(attrId); String natName = attrName; Short natBaseId; if ((attrBaseId == null) || (attrBaseId.length() <= 0)) { natBaseId = null; } else { natBaseId = new Short(Short.parseShort(attrBaseId)); } int natRevision = Integer.parseInt(attrRevision); // Get the parent context CFLibXmlCoreContext parentContext = curContext.getPrevContext(); // Instantiate a buffer for the parsed information ICFGenKbDefClassObj obj = schemaObj.getDefClassTableObj().newInstance(); CFGenKbDefClassBuff dataBuff = obj.getDefClassBuff(); dataBuff.setRequiredId(natId); dataBuff.setRequiredName(natName); dataBuff.setOptionalBaseId(natBaseId); dataBuff.setRequiredRevision(natRevision); obj.copyBuffToPKey(); SortedMap<CFGenKbDefClassPKey, ICFGenKbDefClassObj> sortedMap = (SortedMap<CFGenKbDefClassPKey, ICFGenKbDefClassObj>) xmsgRspnHandler .getSortedMapOfObjects(); ICFGenKbDefClassObj realized = (ICFGenKbDefClassObj) obj.realize(); xmsgRspnHandler.setLastObjectProcessed(realized); if (sortedMap != null) { sortedMap.put(realized.getPKey(), realized); } } catch (RuntimeException e) { throw new RuntimeException("Near " + getParser().getLocationInfo() + ": Caught and rethrew " + e.getClass().getName() + " - " + e.getMessage(), e); } catch (Error e) { throw new Error("Near " + getParser().getLocationInfo() + ": Caught and rethrew " + e.getClass().getName() + " - " + e.getMessage(), e); } }
From source file:net.sourceforge.msscodefactory.cfcore.v2_0.CFGenKbXMsgRqstHandler.CFGenKbXMsgRqstGenBindDeleteByAltIdxHandler.java
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { try {/*from ww w . jav a 2s .com*/ // Common XML Attributes String attrId = null; String attrName = null; String attrToolSetId = null; String attrScopeDefId = null; String attrGenDefId = null; // Attribute Extraction String attrLocalName; int numAttrs; int idxAttr; final String S_ProcName = "startElement"; final String S_LocalName = "LocalName"; assert qName.equals("RqstGenItemDeleteByAltIdx"); CFGenKbXMsgRqstHandler xmsgRqstHandler = (CFGenKbXMsgRqstHandler) getParser(); if (xmsgRqstHandler == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser()"); } ICFGenKbSchemaObj schemaObj = xmsgRqstHandler.getSchemaObj(); if (schemaObj == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser().getSchemaObj()"); } // Extract Attributes numAttrs = attrs.getLength(); for (idxAttr = 0; idxAttr < numAttrs; idxAttr++) { attrLocalName = attrs.getLocalName(idxAttr); if (attrLocalName.equals("Id")) { if (attrId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("Name")) { if (attrName != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrName = attrs.getValue(idxAttr); } else if (attrLocalName.equals("ToolSetId")) { if (attrToolSetId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrToolSetId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("ScopeDefId")) { if (attrScopeDefId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrScopeDefId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("GenDefId")) { if (attrGenDefId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrGenDefId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("schemaLocation")) { // ignored } else { throw CFLib.getDefaultExceptionFactory().newUnrecognizedAttributeException(getClass(), S_ProcName, getParser().getLocationInfo(), attrLocalName); } } // Ensure that required attributes have values$implRqstTableDeleteByHandlerCheckReqKeyAttrs$ // Save named attributes to context CFLibXmlCoreContext curContext = getParser().getCurContext(); // Convert string attributes to native Java types String natName; natName = attrName; short natToolSetId; natToolSetId = Short.parseShort(attrToolSetId); Short natScopeDefId; natScopeDefId = new Short(Short.parseShort(attrScopeDefId)); short natGenDefId; natGenDefId = Short.parseShort(attrGenDefId); // Delete the objects schemaObj.getGenBindTableObj().deleteGenBindByAltIdx(natName, natToolSetId, natScopeDefId, natGenDefId); String response = CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFGenKbXMsgGenItemMessageFormatter.formatGenItemRspnDeleted() + "\n" + CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPostamble(); ((CFGenKbXMsgRqstHandler) getParser()).appendResponse(response); } catch (RuntimeException e) { String response = CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFGenKbXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPostamble(); CFGenKbXMsgRqstHandler xmsgRqstHandler = ((CFGenKbXMsgRqstHandler) getParser()); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (Error e) { String response = CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFGenKbXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPostamble(); CFGenKbXMsgRqstHandler xmsgRqstHandler = ((CFGenKbXMsgRqstHandler) getParser()); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } }
From source file:net.sourceforge.msscodefactory.cfcore.v2_0.CFGenKbXMsgRqstHandler.CFGenKbXMsgRqstGenFileDeleteByAltIdxHandler.java
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { try {/* ww w. j a v a 2 s . c o m*/ // Common XML Attributes String attrId = null; String attrName = null; String attrToolSetId = null; String attrScopeDefId = null; String attrGenDefId = null; // Attribute Extraction String attrLocalName; int numAttrs; int idxAttr; final String S_ProcName = "startElement"; final String S_LocalName = "LocalName"; assert qName.equals("RqstGenItemDeleteByAltIdx"); CFGenKbXMsgRqstHandler xmsgRqstHandler = (CFGenKbXMsgRqstHandler) getParser(); if (xmsgRqstHandler == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser()"); } ICFGenKbSchemaObj schemaObj = xmsgRqstHandler.getSchemaObj(); if (schemaObj == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser().getSchemaObj()"); } // Extract Attributes numAttrs = attrs.getLength(); for (idxAttr = 0; idxAttr < numAttrs; idxAttr++) { attrLocalName = attrs.getLocalName(idxAttr); if (attrLocalName.equals("Id")) { if (attrId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("Name")) { if (attrName != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrName = attrs.getValue(idxAttr); } else if (attrLocalName.equals("ToolSetId")) { if (attrToolSetId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrToolSetId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("ScopeDefId")) { if (attrScopeDefId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrScopeDefId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("GenDefId")) { if (attrGenDefId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrGenDefId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("schemaLocation")) { // ignored } else { throw CFLib.getDefaultExceptionFactory().newUnrecognizedAttributeException(getClass(), S_ProcName, getParser().getLocationInfo(), attrLocalName); } } // Ensure that required attributes have values$implRqstTableDeleteByHandlerCheckReqKeyAttrs$ // Save named attributes to context CFLibXmlCoreContext curContext = getParser().getCurContext(); // Convert string attributes to native Java types String natName; natName = attrName; short natToolSetId; natToolSetId = Short.parseShort(attrToolSetId); Short natScopeDefId; natScopeDefId = new Short(Short.parseShort(attrScopeDefId)); short natGenDefId; natGenDefId = Short.parseShort(attrGenDefId); // Delete the objects schemaObj.getGenFileTableObj().deleteGenFileByAltIdx(natName, natToolSetId, natScopeDefId, natGenDefId); String response = CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFGenKbXMsgGenItemMessageFormatter.formatGenItemRspnDeleted() + "\n" + CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPostamble(); ((CFGenKbXMsgRqstHandler) getParser()).appendResponse(response); } catch (RuntimeException e) { String response = CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFGenKbXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPostamble(); CFGenKbXMsgRqstHandler xmsgRqstHandler = ((CFGenKbXMsgRqstHandler) getParser()); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (Error e) { String response = CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFGenKbXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + CFGenKbXMsgSchemaMessageFormatter.formatRspnXmlPostamble(); CFGenKbXMsgRqstHandler xmsgRqstHandler = ((CFGenKbXMsgRqstHandler) getParser()); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } }