Java Number Convert convertCount(Object obj)

Here you can find the source of convertCount(Object obj)

Description

This method safely converts the result of a SELECT COUNT(*) to an int.

License

Open Source License

Parameter

Parameter Description
obj Object

Return

int

Declaration

public static int convertCount(Object obj) 

Method Source Code

//package com.java2s;
/*/* ww w .  jav  a 2  s.c  om*/
 * Copyright (c) 2008-2011 Simon Ritchie.
 * All rights reserved. 
 * 
 * This program 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 of the License, or 
 * (at your option) any later version.
 * 
 * This program 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 for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program.  If not, see http://www.gnu.org/licenses/>.
 */

import java.math.*;

public class Main {
    /**
     * This method safely converts the result of a SELECT COUNT(*) to an int.
     * 
     * @param obj Object
     * @return int
     */
    public static int convertCount(Object obj) {
        if (obj instanceof Integer) {
            return (Integer) obj;
        } else if (obj instanceof Long) {
            return ((Long) obj).intValue();
        } else if (obj instanceof BigDecimal) {
            return ((BigDecimal) obj).intValue();
        }
        throw new RuntimeException("Cannot convert type " + obj.getClass()
                + " to an int");
    }
}

Related

  1. convert(long value, long factor, int comma)
  2. convertBytesToKBytes(String value)
  3. convertBytesToUnit(long bytes)
  4. convertDurationToMillis(String time)
  5. convertedFileSize(Long fileSize)
  6. convertFromMsToMinutes(Integer duration)
  7. convertGeoCoordinateToDouble(int point)