
org.neo4j.unsafe.impl.batchimport.Utils Maven / Gradle / Ivy
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j 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, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.unsafe.impl.batchimport;
import java.util.NoSuchElementException;
import org.neo4j.unsafe.impl.batchimport.input.InputNode;
import org.neo4j.unsafe.impl.batchimport.input.SourceInputIterator;
/**
* Common and cross-concern utilities.
*/
public class Utils
{
public static int safeCastLongToInt( long value )
{
if ( value > Integer.MAX_VALUE )
{
throw new ArithmeticException( getOverflowMessage( value, Integer.TYPE ) );
}
return (int) value;
}
public static short safeCastLongToShort( long value )
{
if ( value > Short.MAX_VALUE )
{
throw new ArithmeticException( getOverflowMessage( value, Short.TYPE ) );
}
return (short) value;
}
public static byte safeCastLongToByte( long value )
{
if ( value > Byte.MAX_VALUE )
{
throw new ArithmeticException( getOverflowMessage( value, Byte.TYPE ) );
}
return (byte) value;
}
public enum CompareType
{
EQ, GT, GE, LT, LE, NE
};
public static boolean unsignedCompare( long dataA, long dataB, CompareType compareType )
{ // works for signed and unsigned values
switch ( compareType )
{
case EQ:
return (dataA == dataB);
case GE:
if ( dataA == dataB )
{
return true;
}
// fall through to GT
case GT:
return !((dataA < dataB) ^ ((dataA < 0) != (dataB < 0)));
case LE:
if ( dataA == dataB )
{
return true;
}
// fall through to LT
case LT:
return ((dataA < dataB) ^ ((dataA < 0) != (dataB < 0)));
case NE:
}
return false;
}
/**
* Like {@link #unsignedCompare(long, long, CompareType)} but reversed in that you get {@link CompareType}
* from comparing data A and B, i.e. the difference between them.
*/
public static CompareType unsignedDifference( long dataA, long dataB )
{
if ( dataA == dataB )
{
return CompareType.EQ;
}
return ((dataA < dataB) ^ ((dataA < 0) != (dataB < 0))) ? CompareType.LT : CompareType.GT;
}
public static InputIterable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy