Java Object to Short castShort(Object o)

Here you can find the source of castShort(Object o)

Description

Casts a value to a short.

License

Open Source License

Parameter

Parameter Description
o a parameter

Declaration

public static Short castShort(Object o) 

Method Source Code

//package com.java2s;
/* Copyright (C) 2011 Garrett Fleenor
    /*from  w w  w . j  ava2s . co  m*/
 This library is free software; you can redistribute it and/or modify it
 under the terms of the GNU Lesser General Public License as published by
 the Free Software Foundation; either version 3.0 of the License, or (at
 your option) any later version.
    
 This library is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 License (COPYING.txt) for more details.
    
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation,
 Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
 This file relicensed with permission from the Spout Project (www.github.com/SpoutDev)
    
 */

public class Main {
    /**
     * Casts a value to a short. May return null.
     *
     * @param o
     * @return
     */
    public static Short castShort(Object o) {
        if (o == null) {
            return null;
        }

        if (o instanceof Number) {
            return ((Number) o).shortValue();
        }

        try {
            return Short.valueOf(o.toString());
        } catch (NumberFormatException e) {
            return null;
        }
    }
}

Related

  1. castShort(Object o)
  2. castShort(Object val)
  3. castShortToFloat(short[] x)
  4. castToShort(final int value)