collections.sortable
Class SortableProduct

java.lang.Object
  extended by collections.sortable.SortableProduct
All Implemented Interfaces:
java.lang.Comparable

public class SortableProduct
extends java.lang.Object
implements java.lang.Comparable

Demonstrates how to make a class sortable, by implementing the Comparable interface to make the class comparable, and by using the Comparator interface to create explicit comparators.

Author:
Thomas Rowland

Field Summary
static java.util.Comparator PRICE_COMPARATOR
           
static java.util.Comparator QTY_COMPARATOR
           
 
Constructor Summary
SortableProduct(int _id, java.lang.String _name, java.lang.String _descr, int _qty, double _price)
           
 
Method Summary
 int compareTo(java.lang.Object o)
          Overridden method in the Comparator interface makes the class comparable.
 boolean equals(java.lang.Object o)
           
 java.lang.String getDescr()
           
 int getId()
           
 java.lang.String getName()
           
 double getPriceEa()
           
 double getPriceNet()
           
 int getQty()
           
 int hashCode()
           
 boolean isGreater(java.lang.Object o)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

PRICE_COMPARATOR

public static final java.util.Comparator PRICE_COMPARATOR

QTY_COMPARATOR

public static final java.util.Comparator QTY_COMPARATOR
Constructor Detail

SortableProduct

public SortableProduct(int _id,
                       java.lang.String _name,
                       java.lang.String _descr,
                       int _qty,
                       double _price)
Method Detail

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(java.lang.Object o)
Overridden method in the Comparator interface makes the class comparable. Defines the Natural Ordering and must be consistent with equals.

Specified by:
compareTo in interface java.lang.Comparable

isGreater

public boolean isGreater(java.lang.Object o)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getId

public int getId()

getName

public java.lang.String getName()

getDescr

public java.lang.String getDescr()

getQty

public int getQty()

getPriceEa

public double getPriceEa()

getPriceNet

public double getPriceNet()