View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  package org.opencastproject.workflow.conditionparser;
22  
23  import java.util.Objects;
24  
25  /**
26   * Represents a "value" in the condition language, so a string or a number, with corresponding comparison functions
27   */
28  public final class Atom implements Comparable<Atom> {
29    private final Double number;
30    private final String string;
31  
32    private Atom(Double number, String string) {
33      this.number = number;
34      this.string = string;
35    }
36  
37    @Override
38    public String toString() {
39      if (number != null) {
40        final String s = String.valueOf(number);
41        // This is a simple little hack to support something like 3+'4' to be converted to '34' instead of '3.04'. It's
42        // dirty, but you maybe shouldn't be doing 3+'4' anyways.
43        if (s.endsWith(".0")) {
44          return s.substring(0, s.length() - 2);
45        }
46        return s;
47      }
48      return string;
49    }
50  
51    private static Atom fromNumber(double number) {
52      return new Atom(number, null);
53    }
54  
55    static Atom fromString(String string) {
56      return new Atom(null, string);
57    }
58  
59    static Atom parseNumber(String text) {
60      return new Atom(Double.parseDouble(text), null);
61    }
62  
63    static Atom parseString(String text) {
64      return new Atom(null, text);
65    }
66  
67    Atom reduce(Atom atom, NumericalOperator op) {
68      switch (op) {
69        case ADD:
70          if (number != null && atom.number != null) {
71            return Atom.fromNumber(number + atom.number);
72          }
73          return Atom.fromString(toString() + atom.toString());
74        case SUBTRACT:
75          if (number != null) {
76            return Atom.fromNumber(number - atom.number);
77          }
78          throw new IllegalArgumentException("Tried to subtract from string '" + string + "'");
79        case MULTIPLY:
80          if (number != null) {
81            return Atom.fromNumber(number * atom.number);
82          }
83          throw new IllegalArgumentException("Tried to multiply with string '" + string + "'");
84        default:
85          if (number != null) {
86            return Atom.fromNumber(number / atom.number);
87          }
88          throw new IllegalArgumentException("Tried to divide from string '" + string + "'");
89      }
90    }
91  
92    @Override
93    public boolean equals(Object o) {
94      if (this == o)
95        return true;
96      if (o == null || getClass() != o.getClass())
97        return false;
98      Atom atom = (Atom) o;
99      return Objects.equals(number, atom.number) && Objects.equals(string, atom.string);
100   }
101 
102   @Override
103   public int hashCode() {
104     return Objects.hash(number, string);
105   }
106 
107   @Override
108   public int compareTo(Atom o) {
109     if (number != null) {
110       if (o.number == null) {
111         return toString().compareTo(o.string);
112       }
113       return number.compareTo(o.number);
114     }
115     if (o.string == null) {
116       return string.compareTo(o.toString());
117     }
118     return string.compareTo(o.string);
119   }
120 }