Source code for FiniteCyclicGroupElement.java

What follows is the highlighted source code with comments. You can also download this file directly: FiniteCyclicGroupElement.java.

/*
 * This work by W. Patrick Hooper is free of known copyright restrictions.
 * The work is in the public domain.
 * 
 * Author's website: <a href="http://wphooper.com">http://wphooper.com</a>. 
 */
package finitecyclicgroup;

/**
 *
 * @author pat
 */
public class FiniteCyclicGroupElement extends Number {
    private final int i;
    private final FiniteCyclicGroup g;
    
    public FiniteCyclicGroupElement(int mod, Number value) {
        this(new FiniteCyclicGroup(mod),value);
    }
    
    public FiniteCyclicGroupElement(FiniteCyclicGroup g, Number value) {
        this.g=g;
        int j=value.intValue();
        if (j>=0) {
            j= j % g.getSize();
        } else {
            j = j % g.getSize();
            if (j<0) {
                j = j+ g.getSize();
            }
        }
        i=j;
    }

    
    public double doubleValue() {
        return i;
    }

    
    public float floatValue() {
        return i;
    }

    
    public int intValue() {
        return i;
    }

    
    public long longValue() {
        return i;
    }

    
    public FiniteCyclicGroup getAdditiveGroup() {
        return g;
    }

    
    public FiniteCyclicGroupElement minus(Number t) {
        return g.subtract(this, t);
    }

    
    public FiniteCyclicGroupElement negate() {
        return g.negate(this);
    }

    
    public FiniteCyclicGroupElement plus(Number t) {
        return g.add(this, t);
    }

    
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final FiniteCyclicGroupElement other = (FiniteCyclicGroupElement) obj;
        if (this.i != other.i) {
            return false;
        }
        if (this.g != other.g && (this.g == null || !this.g.equals(other.g))) {
            return false;
        }
        return true;
    }

    
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + this.i;
        hash = 59 * hash + (this.g != null ? this.g.hashCode() : 0);
        return hash;
    }

    
    public String toString() {
        return "FiniteCyclicGroupElement{" + i + " mod "+g.getSize()+'}';
    }
    
    
    
}
HOOPER >>>>> JAVA TUTORIAL
Last modified on August 17, 2018.
[check html] [check css]