@Test public void testComparableValues() { int result = GenericComparator.INSTANCE.compare( "string", "string" ); assertThat( result, equalTo( 0 ) ); result = GenericComparator.INSTANCE.compare( new InternalValue( "string" ), "string" ); assertThat( result, equalTo( 0 ) ); }
public int compare( final Object value1, final Object value2 ) { if ( value1 == null && value2 == null ) { return 0; } if ( value1 == null ) { return -1; } if ( value2 == null ) { return +1; } if ( value1 instanceof Number && value2 instanceof Number ) { final Number n1 = (Number) value1; final Number n2 = (Number) value2; final double d1 = n1.doubleValue(); final double d2 = n2.doubleValue(); return Double.compare( d1, d2 ); } if ( value1 instanceof Comparable && value2 instanceof Comparable ) { try { final Comparable<Object> c1 = (Comparable<Object>) value1; final Comparable<Object> c2 = (Comparable<Object>) value2; return c1.compareTo( c2 ); } catch ( final Exception cce ) { } } final String s1 = String.valueOf( value1 ); final String s2 = String.valueOf( value2 ); return s1.compareTo( s2 ); }
GenericComparator implements Comparator<Object> { public int compare( final Object value1, final Object value2 ) { if ( value1 == null && value2 == null ) { return 0; } if ( value1 == null ) { return -1; } if ( value2 == null ) { return +1; } if ( value1 instanceof Number && value2 instanceof Number ) { final Number n1 = (Number) value1; final Number n2 = (Number) value2; final double d1 = n1.doubleValue(); final double d2 = n2.doubleValue(); return Double.compare( d1, d2 ); } if ( value1 instanceof Comparable && value2 instanceof Comparable ) { try { final Comparable<Object> c1 = (Comparable<Object>) value1; final Comparable<Object> c2 = (Comparable<Object>) value2; return c1.compareTo( c2 ); } catch ( final Exception cce ) { } } final String s1 = String.valueOf( value1 ); final String s2 = String.valueOf( value2 ); return s1.compareTo( s2 ); } }
GenericComparator implements Comparator<Object> { public int compare( final Object value1, final Object value2 ) { if ( value1 == null && value2 == null ) { return 0; } if ( value1 == null ) { return -1; } if ( value2 == null ) { return +1; } if ( value1 instanceof Number && value2 instanceof Number ) { final Number n1 = (Number) value1; final Number n2 = (Number) value2; final double d1 = n1.doubleValue(); final double d2 = n2.doubleValue(); return Double.compare( d1, d2 ); } if ( value1 instanceof Comparable && value2 instanceof Comparable ) { try { final Comparable<Object> c1 = (Comparable<Object>) value1; final Comparable<Object> c2 = (Comparable<Object>) value2; return c1.compareTo( c2 ); } catch ( final Exception cce ) { } } final String s1 = String.valueOf( value1 ); final String s2 = String.valueOf( value2 ); return s1.compareTo( s2 ); } GenericComparator(); }
GenericComparator implements Comparator<Object> { public int compare( final Object value1, final Object value2 ) { if ( value1 == null && value2 == null ) { return 0; } if ( value1 == null ) { return -1; } if ( value2 == null ) { return +1; } if ( value1 instanceof Number && value2 instanceof Number ) { final Number n1 = (Number) value1; final Number n2 = (Number) value2; final double d1 = n1.doubleValue(); final double d2 = n2.doubleValue(); return Double.compare( d1, d2 ); } if ( value1 instanceof Comparable && value2 instanceof Comparable ) { try { final Comparable<Object> c1 = (Comparable<Object>) value1; final Comparable<Object> c2 = (Comparable<Object>) value2; return c1.compareTo( c2 ); } catch ( final Exception cce ) { } } final String s1 = String.valueOf( value1 ); final String s2 = String.valueOf( value2 ); return s1.compareTo( s2 ); } GenericComparator(); int compare( final Object value1, final Object value2 ); }
GenericComparator implements Comparator<Object> { public int compare( final Object value1, final Object value2 ) { if ( value1 == null && value2 == null ) { return 0; } if ( value1 == null ) { return -1; } if ( value2 == null ) { return +1; } if ( value1 instanceof Number && value2 instanceof Number ) { final Number n1 = (Number) value1; final Number n2 = (Number) value2; final double d1 = n1.doubleValue(); final double d2 = n2.doubleValue(); return Double.compare( d1, d2 ); } if ( value1 instanceof Comparable && value2 instanceof Comparable ) { try { final Comparable<Object> c1 = (Comparable<Object>) value1; final Comparable<Object> c2 = (Comparable<Object>) value2; return c1.compareTo( c2 ); } catch ( final Exception cce ) { } } final String s1 = String.valueOf( value1 ); final String s2 = String.valueOf( value2 ); return s1.compareTo( s2 ); } GenericComparator(); int compare( final Object value1, final Object value2 ); static final GenericComparator INSTANCE; }