Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify numeric types conversion utilities. Introduce Numbers.
Remove duplicated conversions across code base that perform safe conversion from one numeric type to another. Unify them and cleanup.
- Loading branch information
1 parent
f9f24f4
commit 8d0ba32
Showing
27 changed files
with
235 additions
and
141 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
community/common/src/main/java/org/neo4j/helpers/Numbers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.helpers; | ||
|
||
public class Numbers | ||
{ | ||
public static short safeCastIntToUnsignedShort( int value ) | ||
{ | ||
if ( (value & ~0xFFFF) != 0 ) | ||
{ | ||
throw new ArithmeticException( getOverflowMessage( value, "unsigned short" ) ); | ||
} | ||
return (short) value; | ||
} | ||
|
||
public static int safeCastLongToInt( long value ) | ||
{ | ||
if ( (int) value != value ) | ||
{ | ||
throw new ArithmeticException( getOverflowMessage( value, Integer.TYPE ) ); | ||
} | ||
return (int) value; | ||
} | ||
|
||
public static short safeCastLongToShort( long value ) | ||
{ | ||
if ( (short) value != value ) | ||
{ | ||
throw new ArithmeticException( getOverflowMessage( value, Short.TYPE ) ); | ||
} | ||
return (short) value; | ||
} | ||
|
||
public static byte safeCastLongToByte( long value ) | ||
{ | ||
if ( (byte) value != value ) | ||
{ | ||
throw new ArithmeticException( getOverflowMessage( value, Byte.TYPE ) ); | ||
} | ||
return (byte) value; | ||
} | ||
|
||
public static int unsignedShortToInt( short value ) | ||
{ | ||
return value & 0xFFFF; | ||
} | ||
|
||
private static String getOverflowMessage( long value, Class clazz ) | ||
{ | ||
return getOverflowMessage( value, clazz.getName() ); | ||
} | ||
|
||
private static String getOverflowMessage( long value, String numericType ) | ||
{ | ||
return "Value " + value + " is too big to be represented as " + numericType; | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
community/common/src/test/java/org/neo4j/helpers/NumbersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.helpers; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.neo4j.helpers.Numbers.safeCastIntToUnsignedShort; | ||
import static org.neo4j.helpers.Numbers.safeCastLongToByte; | ||
import static org.neo4j.helpers.Numbers.safeCastLongToInt; | ||
import static org.neo4j.helpers.Numbers.safeCastLongToShort; | ||
import static org.neo4j.helpers.Numbers.unsignedShortToInt; | ||
|
||
public class NumbersTest | ||
{ | ||
|
||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
@Test | ||
public void failSafeCastLongToInt() | ||
{ | ||
expectedException.expect( ArithmeticException.class ); | ||
expectedException.expectMessage( "Value 2147483648 is too big to be represented as int" ); | ||
|
||
safeCastLongToInt( Integer.MAX_VALUE + 1L ); | ||
} | ||
|
||
@Test | ||
public void failSafeCastLongToShort() | ||
{ | ||
expectedException.expect( ArithmeticException.class ); | ||
expectedException.expectMessage( "Value 32768 is too big to be represented as short" ); | ||
|
||
safeCastLongToShort( Short.MAX_VALUE + 1L ); | ||
} | ||
|
||
@Test | ||
public void failSafeCastIntToUnsignedShort() | ||
{ | ||
expectedException.expect( ArithmeticException.class ); | ||
expectedException.expectMessage( "Value 131068 is too big to be represented as unsigned short" ); | ||
|
||
safeCastIntToUnsignedShort( Short.MAX_VALUE << 1 + 1 ); | ||
} | ||
|
||
@Test | ||
public void failSafeCastLongToByte() | ||
{ | ||
expectedException.expect( ArithmeticException.class ); | ||
expectedException.expectMessage( "Value 128 is too big to be represented as byte" ); | ||
|
||
safeCastLongToByte( Byte.MAX_VALUE + 1 ); | ||
} | ||
|
||
@Test | ||
public void castLongToInt() | ||
{ | ||
assertEquals(1, safeCastLongToInt( 1L )); | ||
assertEquals(10, safeCastLongToInt( 10L )); | ||
assertEquals(-1, safeCastLongToInt( -1L )); | ||
assertEquals(Integer.MAX_VALUE, safeCastLongToInt( Integer.MAX_VALUE )); | ||
assertEquals(Integer.MIN_VALUE, safeCastLongToInt( Integer.MIN_VALUE )); | ||
} | ||
|
||
@Test | ||
public void castLongToShort() | ||
{ | ||
assertEquals(1, safeCastLongToShort( 1L )); | ||
assertEquals(10, safeCastLongToShort( 10L )); | ||
assertEquals(-1, safeCastLongToShort( -1L )); | ||
assertEquals(Short.MAX_VALUE, safeCastLongToShort( Short.MAX_VALUE )); | ||
assertEquals(Short.MIN_VALUE, safeCastLongToShort( Short.MIN_VALUE )); | ||
} | ||
|
||
@Test | ||
public void castIntToUnsighedShort() | ||
{ | ||
assertEquals(1, safeCastIntToUnsignedShort( 1 )); | ||
assertEquals(10, safeCastIntToUnsignedShort( 10 )); | ||
assertEquals(Short.MAX_VALUE, safeCastIntToUnsignedShort( Short.MAX_VALUE )); | ||
} | ||
|
||
@Test | ||
public void castLongToByte() | ||
{ | ||
assertEquals(1, safeCastLongToByte( 1L )); | ||
assertEquals(10, safeCastLongToByte( 10L )); | ||
assertEquals(-1, safeCastLongToByte( -1L )); | ||
assertEquals(Byte.MAX_VALUE, safeCastLongToByte( Byte.MAX_VALUE )); | ||
assertEquals(Byte.MIN_VALUE, safeCastLongToByte( Byte.MIN_VALUE )); | ||
} | ||
|
||
@Test | ||
public void castUnsignedShortToInt() | ||
{ | ||
assertEquals( 1, unsignedShortToInt( (short) 1 ) ); | ||
assertEquals( Short.MAX_VALUE, unsignedShortToInt( Short.MAX_VALUE ) ); | ||
assertEquals( (Short.MAX_VALUE << 1) | 1, unsignedShortToInt( (short) -1 ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.