Java Long to Int longToInt(Long l)

Here you can find the source of longToInt(Long l)

Description

Converts a Long to an int with special attention to overflow issues.

License

Open Source License

Return

The converted int. If the Long is larger than Integer.MAX_VALUE, it returns Integer.MAX_VALUE. If the Long is smaller than Integer.MIN_VALUE, it returns Integer.MIN_VALUE. If the parameter is null, it returns 0.

Declaration

public static int longToInt(Long l) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   ww  w .j a  v  a2  s  .c  o m*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

public class Main {
    /**
     * Converts a Long to an int with special attention to overflow issues.
     *
     * @return The converted int. If the Long is larger than Integer.MAX_VALUE, it returns Integer.MAX_VALUE. If the Long
     *         is smaller than Integer.MIN_VALUE, it returns Integer.MIN_VALUE. If the parameter is <code>null</code>, it
     *         returns 0.
     */
    public static int longToInt(Long l) {
        if (l != null) {
            if (new Long(Integer.MAX_VALUE).compareTo(l) == -1) {
                return Integer.MAX_VALUE;
            } else if (new Long(Integer.MIN_VALUE).compareTo(l) == 1) {
                return Integer.MIN_VALUE;
            }
            return l.intValue();
        }
        return 0;
    }
}

Related

  1. Long2Int(long i)
  2. long2int(Long source)
  3. longToInt(final Long l, final String name, final int deflt)
  4. longToInt(long d)
  5. longToInt(long l)
  6. longToInt(Long l)
  7. longToInt(long value)
  8. longToInt(long[] values)
  9. longToIntArr(long[] longArr)