1 // Komi.java 2 3 package net.sf.gogui.go; 4 5 import java.text.DecimalFormat; 6 import java.text.NumberFormat; 7 import java.util.Locale; 8 9 /** Value of komi. 10 This class is immutable. */ 11 public final class Komi 12 { 13 /** Constructor. 14 @param komi The value for the komi. */ Komi(double komi)15 public Komi(double komi) 16 { 17 m_value = komi; 18 } 19 equals(Object object)20 public boolean equals(Object object) 21 { 22 if (object == null || object.getClass() != getClass()) 23 return false; 24 Komi komi = (Komi)object; 25 return (komi.m_value == m_value); 26 } 27 hashCode()28 public int hashCode() 29 { 30 // As in Double.hashCode() 31 long v = Double.doubleToLongBits(m_value); 32 return (int)(v ^ (v >>> 32)); 33 } 34 isMultipleOf(double multiple)35 public boolean isMultipleOf(double multiple) 36 { 37 return Math.IEEEremainder(m_value, multiple) == 0; 38 } 39 40 /** Parse komi from string. 41 @param s The string (null not allowed), empty string means unknown 42 komi. 43 @return The komi or null if unknown komi. */ parseKomi(String s)44 public static Komi parseKomi(String s) throws InvalidKomiException 45 { 46 assert s != null; 47 if (s.trim().equals("")) 48 return null; 49 try 50 { 51 // Also accept , instead of . 52 double komi = Double.parseDouble(s.replace(',', '.')); 53 return new Komi(komi); 54 } 55 catch (NumberFormatException e) 56 { 57 throw new InvalidKomiException(s); 58 } 59 } 60 toDouble()61 public double toDouble() 62 { 63 return m_value; 64 } 65 66 /** Like Komi.toString() but interprets null argument as zero komi. */ toString(Komi komi)67 static public String toString(Komi komi) 68 { 69 if (komi == null) 70 return "0"; 71 return komi.toString(); 72 } 73 toString()74 public String toString() 75 { 76 DecimalFormat format = 77 (DecimalFormat)(NumberFormat.getInstance(Locale.ENGLISH)); 78 format.setGroupingUsed(false); 79 format.setDecimalSeparatorAlwaysShown(false); 80 return format.format(m_value); 81 } 82 83 private final double m_value; 84 } 85