Source of Measurable.java


  1: import java.util.Comparator;

  3: /**
  4:  * A type for objects that have an area and a perimeter. An example of this is
  5:  * a plane figure, such as a {@link Circle} or {@link Rectangle}.
  6:  * <p>
  7:  * This version has been modified to add some static methods and constants.
  8:  *
  9:  * @author Mark Young (A00000000)
 10:  */
 11: public interface Measurable {

 13:     /**
 14:      * The area of the Measurable object. That is, the amount of space it 
 15:      * covers on the plane.
 16:      */
 17:     public double getArea();

 19:     /**
 20:      * The perimeter of the Measurable object. That is, the total distance
 21:      * around its edge.
 22:      */
 23:     public double getPerimeter();

 25:     /**
 26:      * How round this object is. A circle is perfectly round (1.0), while a
 27:      * square is only moderately round (0.8), while a long, thin rectangle
 28:      * may have a roundness close to zero.
 29:      *
 30:      * @param m the object whose roundness is to be calculated
 31:      * @return the roundness of {@code m} as a number in the range [0,1]
 32:      */
 33:     public static double roundness(Measurable m) {
 34:         return 4 * Math.PI * m.getArea() / Math.pow(m.getPerimeter(), 2);
 35:     }

 37:     /**
 38:      * A {link java.util.Comparator Comparator} to allow sorting by area.
 39:      */
 40:     public static final Comparator<Measurable> BY_AREA =
 41:             (a, b) -> Double.compare(a.getArea(), b.getArea());

 43:     /**
 44:      * A {link java.util.Comparator Comparator} to allow sorting by perimeter.
 45:      */
 46:     public static final Comparator<Measurable> BY_PERIMETER =
 47:             (a, b) -> Double.compare(a.getPerimeter(), b.getPerimeter());

 49: }