Java Array Truncate truncateAndConvertToInt(long[] longArray)

Here you can find the source of truncateAndConvertToInt(long[] longArray)

Description

Converts long[] to int[], truncating to maximum/minimum value if needed.

License

Open Source License

Parameter

Parameter Description
longArray a parameter

Declaration

public static int[] truncateAndConvertToInt(long[] longArray) 

Method Source Code

//package com.java2s;
/*/*from  ww w .  ja  v  a 2  s. c  om*/
  Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
    
  The MySQL Connector/J is licensed under the terms of the GPLv2
  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
  There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
  this software, see the FOSS License Exception
  <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
    
  This program is free software; you can redistribute it and/or modify it under the terms
  of the GNU General Public License as published by the Free Software Foundation; version 2
  of the License.
    
  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 General Public License for more details.
    
  You should have received a copy of the GNU General Public License along with this
  program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
  Floor, Boston, MA 02110-1301  USA
    
 */

public class Main {
    /**
     * Converts long to int, truncating to maximum/minimum value if needed.
     * 
     * @param longValue
     * @return
     */
    public static int truncateAndConvertToInt(long longValue) {
        return longValue > Integer.MAX_VALUE ? Integer.MAX_VALUE
                : longValue < Integer.MIN_VALUE ? Integer.MIN_VALUE : (int) longValue;
    }

    /**
     * Converts long[] to int[], truncating to maximum/minimum value if needed.
     * 
     * @param longArray
     * @return
     */
    public static int[] truncateAndConvertToInt(long[] longArray) {
        int[] intArray = new int[longArray.length];

        for (int i = 0; i < longArray.length; i++) {
            intArray[i] = longArray[i] > Integer.MAX_VALUE ? Integer.MAX_VALUE
                    : longArray[i] < Integer.MIN_VALUE ? Integer.MIN_VALUE : (int) longArray[i];
        }
        return intArray;
    }
}

Related

  1. truncate(byte[] nextPartBytes, int partSize)
  2. truncate(final byte[] arr, final int len)
  3. truncate(int[] array, int maxLen)
  4. truncate(int[] values, int value)
  5. truncate(short[] value, int maxSize)
  6. truncateBytes(byte[] inputBytes)
  7. truncateByteSegment(byte[] byteSegment, int toSize)
  8. truncateData(byte[] data, int numOfBytes)
  9. truncateHashToLong(byte[] bytes)