View Javadoc

1   package net.sf.jhunlang.jmorph.sword;
2   
3   import java.util.Iterator;
4   import java.util.Map;
5   import java.util.TreeMap;
6   
7   import java.io.Serializable;
8   
9   import net.sf.jhunlang.jmorph.util.Comparables;
10  
11  /***
12   * Case instances represent prefix and affix categories.
13   * They are mapped by their {@link #name}s.
14   */ 
15  public class Case implements Serializable
16  {
17    public final static String[] ZERO_MORPHS =
18    {
19      "NOM",
20      "PRES_INDIC_INDEF_SG_3"
21    };
22    
23    /***
24     * The map of Case instances by their {@link #name}s.
25     */
26    protected final static Map cases = new TreeMap();
27    /***
28     * Convenience constant for the empty Case.
29     */
30    public final static Case empty = getCase("");
31    /***
32     * The affix morpheme 
33     */
34    protected String affix;
35    /***
36     * The morpheme (allomorph) of this Case.
37     */
38    protected String name;
39    
40    protected boolean zero;
41  
42    /***
43     * Return a Case instance for <code>name</code> with zero morpheme.
44     * Return the Case instance from {@link #cases} for <code>name</code> if any.
45     * Create and return a new one otherwise.
46     * @param name the name of the case
47     * @return the Case instance for <code>name</code> with zero morpheme 
48     */
49    public static Case getCase(String name)
50    {
51      return getCase("", name);
52    }
53  
54    /***
55     * Return a Case instance for <code>name</code> with allomorph
56     * <code>affix</code>. Return the Case instance from {@link #cases}
57     * for <code>affix</code> and <code>name</code> if any. Create and
58     * return a new one otherwise.
59     * @param affix the allomorph of the case
60     * @param name the name of the case
61     * @return the Case instance for <code>name</code> with allomorph
62     * <code>affix</code> 
63     */
64    public static Case getCase(String affix, String name)
65    {
66      Comparables c = new Comparables(affix, name, false);
67      Case cas = (Case)cases.get(c);
68      if (cas == null)
69      {
70        for(int i = 0; i < ZERO_MORPHS.length; i++)
71        {
72          if (name.equalsIgnoreCase(ZERO_MORPHS[i]))
73          {
74            cas = new Case(affix, name, true);                   
75          }
76        }
77        if (cas == null)
78        {
79          cas = new Case(affix, name, false);                           
80        }
81        cases.put(c, cas);
82      }
83      return cas;
84    }
85  
86    /***
87     * Return an iterator for all the Case instances created so far.
88     * @return an iterator for all the Case instances created so far
89     */
90    public static Iterator getCaseIterator()
91    {
92      return cases.values().iterator();
93    }
94  
95    /***
96     * Create a Case instance for <code>name</code> with allomorph
97     * <code>affix</code>.
98     * @param affix the allomorph of the case
99     * @param name the name of the case
100    */
101   protected Case(String affix, String name, boolean zero)
102   {
103     this.affix = affix.intern();
104     this.name = name.intern();
105     this.zero = zero;
106   }
107   
108   public boolean zero()
109   {
110     return zero;
111   }
112 
113   /***
114    * Return the name of this Case instance.
115    * @return the name of this Case instance
116    */
117   public String getName()
118   {
119     return name;
120   }
121 
122   /***
123    * Return the allomorph of this Case instance.
124    * @return the allomorph of this Case instance
125    */
126   public String getAffix()
127   {
128     return affix;
129   }
130 
131   public String morphString()
132   {
133     return affix.length() == 0 ? name : (affix + ", " + name);
134   }
135 
136   public String contentString()
137   {
138     return morphString();
139   }
140 
141   public String toString()
142   {
143     return "{" + contentString() + "}";
144   }
145 }
146