(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / TimePeriod.java
index 50db153cef6537708260b6d1d858f1422437531b..83343854da8ead4cd80fbac15ae64bc359efc4ac 100644 (file)
@@ -235,6 +235,19 @@ public class TimePeriod implements Cloneable, Serializable {
                }
        }
        
+       /**
+        * True, if there is no start date and no end date and no freetext representation exists.
+        * @return
+        */
+       @Transient
+       public boolean isEmpty(){
+               if (CdmUtils.isEmpty(this.getFreeText()) && start == null  && end == null ){
+                       return true;
+               }else{
+                       return false;
+               }
+       }
+       
        
        public Partial getStart() {
                return start;
@@ -253,17 +266,23 @@ public class TimePeriod implements Cloneable, Serializable {
        }
        
        /**
+        * For time periods that need to store more information than the one
+        * that can be stored in <code>start</code> and <code>end</code>.
+        * If free text is not <code>null</null> {@link #toString()} will always
+        * return the free text value.
+        * <BR>Use {@link #toString()} for public use.
         * @return the freeText
         */
-       protected String getFreeText() {
+       public String getFreeText() {
                return freeText;
        }
 
 
        /**
+        * Use {@link #parseSingleDate(String)} for public use.
         * @param freeText the freeText to set
         */
-       protected void setFreeText(String freeText) {
+       public void setFreeText(String freeText) {
                this.freeText = freeText;
        }
 
@@ -424,7 +443,7 @@ public class TimePeriod implements Cloneable, Serializable {
        //case fl. 1806 or c. 1806 or fl. 1806?
        private static final Pattern prefixedYearPattern =  Pattern.compile("(fl|c)\\.\\s*\\d{4}(\\s*-\\s*\\d{4})?\\??");
        //standard
-       private static final Pattern standardPattern =  Pattern.compile("\\s*\\d{2,4}(\\s*-\\s*\\d{2,4})?");
+       private static final Pattern standardPattern =  Pattern.compile("\\s*\\d{2,4}(\\s*-(\\s*\\d{2,4})?)?");
        
        
        public static TimePeriod parseString(TimePeriod timePeriod, String periodString){
@@ -441,15 +460,18 @@ public class TimePeriod implements Cloneable, Serializable {
                }
                periodString = periodString.trim();
                
-               result.setFreeText(periodString);
+               result.setFreeText(null);
                
                //case "1806"[1807];
                if (uncorrectYearPatter.matcher(periodString).matches()){
+                       result.setFreeText(periodString);
                        String realYear = periodString.split("\\[")[1];
                        realYear = realYear.replace("]", "");
                        result.setStartYear(Integer.valueOf(realYear));
+                       result.setFreeText(periodString);
                //case fl. 1806 or c. 1806 or fl. 1806?
                }else if(prefixedYearPattern.matcher(periodString).matches()){
+                       result.setFreeText(periodString);
                        Matcher yearMatcher = firstYearPattern.matcher(periodString);
                        yearMatcher.find();
                        String startYear = yearMatcher.group();
@@ -485,9 +507,11 @@ public class TimePeriod implements Cloneable, Serializable {
                                        result.setEnd(dtEnd);
                                } catch (IllegalArgumentException e) {
                                        //logger.warn(e.getMessage());
-                                       //use freetext instead
+                                       result.setFreeText(periodString);
                                }
                        }
+               }else{
+                       result.setFreeText(periodString);
                }
                return result;
        }
@@ -559,14 +583,22 @@ public class TimePeriod implements Cloneable, Serializable {
                
        }
        
+//**************************** to String ****************************************      
+       
+       /** 
+        * Returns the {@link #getFreeText()} value if free text is not <code>null</code>.
+        * Otherwise the concatenation of <code>start</code> and <code>end</code> is returned. 
+        * 
+        * @see java.lang.Object#toString()
+        */
        public String toString(){
                String result = null;
                DateTimeFormatter formatter = new TimePeriodPartialFormatter();
-               if (! CdmUtils.isEmpty(this.getFreeText())){
+               if ( CdmUtils.isNotEmpty(this.getFreeText())){
                        result = this.getFreeText();
                }else{
-                       String strStart = start != null? start.toString(formatter): null;
-                       String strEnd = end != null? end.toString(formatter): null;
+                       String strStart = start != null ? start.toString(formatter): null;
+                       String strEnd = end != null ? end.toString(formatter): null;
                        result = CdmUtils.concat("-", strStart, strEnd);
                }
                return result;