Skip to content

Commit

Permalink
Merge pull request #225 from maarzt/fix-argbtype-equals
Browse files Browse the repository at this point in the history
Add ARGBType::equals
  • Loading branch information
tpietzsch authored Sep 9, 2018
2 parents b4fcd44 + 25e4056 commit 4095c1e
Show file tree
Hide file tree
Showing 33 changed files with 891 additions and 506 deletions.
91 changes: 51 additions & 40 deletions src/main/java/net/imglib2/type/label/BasePairBitType.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,63 @@
*/
public class BasePairBitType extends AbstractBit64Type< BasePairBitType > implements BasePairType< BasePairBitType >
{
public static enum Base { gap, N, A, T, G, C, U; }
// hom many bits a BasePairBitType contains
private static final int NBITS = 3;

public enum Base
{
gap( ' ' ),
N( 'N' ),
A( 'A' ),
T( 'T' ),
G( 'G' ),
C( 'C' ),
U( 'U' );

private final char c;

Base( final char c )
{
this.c = c;
}

public char getChar()
{
return c;
}

public static Base fromChar( final char c )
{
switch ( c )
{
case 'A':
return A;
case 'T':
return T;
case 'G':
return G;
case 'C':
return C;
case 'U':
return U;
case ' ':
return gap;
default:
return N;
}
}
}

// this is the constructor if you want it to read from an array
public BasePairBitType( final NativeImg< ?, ? extends LongAccess > bitStorage )
{
super( bitStorage, 3 );
super( bitStorage, NBITS );
}

// this is the constructor if you want it to be a variable
public BasePairBitType( final Base value )
{
super( value.ordinal() );
super( value.ordinal(), NBITS );
}

// this is the constructor if you want it to be a variable
Expand Down Expand Up @@ -89,29 +134,13 @@ public void set( final Base base )
@Override
public Base get()
{
return Base.values()[ (int)getBits() ];
return Base.values()[ ( int ) getBits() ];
}

@Override
public int compareTo( final BasePairBitType c )
{
final Base input = get();
final Base compare = c.get();

if ( input == compare )
{
return 0;
}
switch ( input )
{
case gap: return -1;
case N: return compare == Base.gap ? 1 : -1;
case A: return compare == Base.gap || compare == Base.N ? 1 : -1;
case T: return compare == Base.G || compare == Base.C || compare == Base.U ? -1 : 1;
case G: return compare == Base.C || compare == Base.U ? -1 : 1;
case C: return compare == Base.U ? -1 : 1;
default: return 1;
}
return get().compareTo( c.get() );
}

@Override
Expand All @@ -132,25 +161,7 @@ public void complement()
@Override
public byte baseToValue()
{
final Base base = get();

switch ( base )
{
case N:
return 1;
case A:
return 2;
case T:
return 3;
case G:
return 4;
case C:
return 5;
case U:
return 6;
default:
return 0;
}
return ( byte ) get().ordinal();
}

@Override
Expand Down
87 changes: 4 additions & 83 deletions src/main/java/net/imglib2/type/label/BasePairCharType.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,54 +124,13 @@ public void setChar( final char f )
@Override
public void set( final Base base )
{
switch ( base )
{
case A:
setChar( 'A' );
return;
case T:
setChar( 'T' );
return;
case G:
setChar( 'G' );
return;
case C:
setChar( 'C' );
return;
case U:
setChar( 'U' );
return;
case gap:
setChar( ' ' );
return;
default:
setChar( 'N' );
return;
}
setChar( base.getChar() );
}

@Override
public Base get()
{
final char value = getChar();

switch ( value )
{
case 'A':
return Base.A;
case 'T':
return Base.T;
case 'G':
return Base.G;
case 'C':
return Base.C;
case 'U':
return Base.U;
case ' ':
return Base.gap;
default:
return Base.N;
}
return Base.fromChar( getChar() );
}

@Override
Expand All @@ -183,27 +142,7 @@ public void set( final BasePairCharType c )
@Override
public int compareTo( final BasePairCharType c )
{
final char input = getChar();
final char compare = c.getChar();

if ( input == compare ) { return 0; }
switch ( input )
{
case ' ':
return -1;
case 'N':
return compare == ' ' ? 1 : -1;
case 'A':
return compare == ' ' || compare == 'N' ? 1 : -1;
case 'T':
return compare == 'G' || compare == 'C' || compare == 'U' ? -1 : 1;
case 'G':
return compare == 'C' || compare == 'U' ? -1 : 1;
case 'C':
return compare == 'U' ? -1 : 1;
default:
return 1;
}
return get().compareTo( c.get() );
}

@Override
Expand Down Expand Up @@ -233,25 +172,7 @@ public void complement()
@Override
public byte baseToValue()
{
final char base = getChar();

switch ( base )
{
case 'N':
return 1;
case 'A':
return 2;
case 'T':
return 3;
case 'G':
return 4;
case 'C':
return 5;
case 'U':
return 6;
default:
return 0;
}
return ( byte ) get().ordinal();
}

@Override
Expand Down
33 changes: 19 additions & 14 deletions src/main/java/net/imglib2/type/logic/BitType.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import net.imglib2.type.numeric.IntegerType;
import net.imglib2.type.numeric.integer.AbstractIntegerType;
import net.imglib2.util.Fraction;
import net.imglib2.util.Util;

/**
* TODO
Expand Down Expand Up @@ -289,20 +290,6 @@ public void dec()
not();
}

@Override
public int compareTo( final BitType c )
{
final boolean b1 = get();
final boolean b2 = c.get();

if ( b1 && !b2 )
return 1;
else if ( !b1 && b2 )
return -1;
else
return 0;
}

@Override
public BitType createVariable()
{
Expand Down Expand Up @@ -371,9 +358,27 @@ public int getBitsPerPixel()
return 1;
}

@Override
public int compareTo( final BitType other )
{
return Boolean.compare( get(), other.get() );
}

@Override
public boolean valueEquals( final BitType t )
{
return get() == t.get();
}

@Override
public boolean equals( final Object obj )
{
return Util.valueEqualsObject( this, obj );
}

@Override
public int hashCode()
{
return Boolean.hashCode( get() );
}
}
30 changes: 20 additions & 10 deletions src/main/java/net/imglib2/type/logic/BoolType.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import net.imglib2.type.BooleanType;
import net.imglib2.type.numeric.integer.AbstractIntegerType;
import net.imglib2.util.Util;

/**
* A {@link BooleanType} wrapping a single primitive {@code boolean} variable.
Expand Down Expand Up @@ -80,15 +81,6 @@ public void set( final BoolType c )
value = c.get();
}

@Override
public int compareTo( final BoolType o )
{
if ( value )
return o.value ? 0 : 1;
else
return o.value ? -1 : 0;
}

@Override
public boolean get()
{
Expand Down Expand Up @@ -193,10 +185,28 @@ public void setBigInteger( final BigInteger b )
{
set( b.compareTo(BigInteger.ZERO) > 0 );
}


@Override
public int compareTo( final BoolType other )
{
return Boolean.compare( get(), other.get() );
}

@Override
public boolean valueEquals( BoolType t )
{
return get() == t.get();
}

@Override
public boolean equals( final Object obj )
{
return Util.valueEqualsObject( this, obj );
}

@Override
public int hashCode()
{
return Boolean.hashCode( get() );
}
}
18 changes: 8 additions & 10 deletions src/main/java/net/imglib2/type/logic/NativeBoolType.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import net.imglib2.type.NativeTypeFactory;
import net.imglib2.type.numeric.integer.AbstractIntegerType;
import net.imglib2.util.Fraction;
import net.imglib2.util.Util;

/**
* {@link NativeType} backed by {@code boolean}.
Expand Down Expand Up @@ -267,24 +268,22 @@ public boolean valueEquals( final NativeBoolType t )
return get() == t.get();
}

@Override
public boolean equals( Object obj )
{
return Util.valueEqualsObject( this, obj );
}

@Override
public int hashCode()
{
// NB: Use the same hash code as java.lang.Boolean#hashCode().
return Boolean.hashCode( get() );
}

@Override
public int compareTo( final NativeBoolType c )
{
final boolean b1 = get();
final boolean b2 = c.get();

if ( b1 && !b2 )
return 1;
if ( !b1 && b2 )
return -1;
return 0;
return Boolean.compare( get(), c.get() );
}

@Override
Expand Down Expand Up @@ -346,5 +345,4 @@ public void setBigInteger( BigInteger b )
{
set( b.compareTo( BigInteger.ZERO ) > 0 );
}

}
Loading

0 comments on commit 4095c1e

Please sign in to comment.