View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl3.internal;
18  
19  import java.util.Objects;
20  
21  import org.apache.commons.jexl3.JexlFeatures;
22  
23  /**
24   * Maintains the set of allowed features associated with a script/expression source.
25   * <p>This is meant for caching scripts using their 'source' as key but still distinguishing
26   * scripts with different features and prevent false sharing.
27   */
28  public final class Source implements Comparable<Source> {
29      /** The hash code, pre-computed for fast op. */
30      private final int hashCode;
31      /** The set of features. */
32      private final JexlFeatures features;
33      /** The actual source script/expression. */
34      private final String str;
35  
36      /**
37       * Default constructor.
38       * @param theFeatures the features
39       * @param theStr the script source
40       */
41      Source(final JexlFeatures theFeatures, final String theStr) { // CSOFF: MagicNumber
42          this.features = theFeatures;
43          this.str = theStr;
44          int hash = 3;
45          hash = 37 * hash + features.hashCode();
46          hash = 37 * hash + str.hashCode() ;
47          this.hashCode = hash;
48      }
49  
50      @Override
51      public int compareTo(final Source s) {
52          return str.compareTo(s.str);
53      }
54  
55      @Override
56      public boolean equals(final Object obj) {
57          if (this == obj) {
58              return true;
59          }
60          if (obj == null) {
61              return false;
62          }
63          if (getClass() != obj.getClass()) {
64              return false;
65          }
66          final Source other = (Source) obj;
67          if (!Objects.equals(this.features, other.features)) {
68              return false;
69          }
70          if (!Objects.equals(this.str, other.str)) {
71              return false;
72          }
73          return true;
74      }
75  
76      /**
77       * @return the features associated with the source
78       */
79      public JexlFeatures getFeatures() {
80          return features;
81      }
82  
83      @Override
84      public int hashCode() {
85          return hashCode;
86      }
87  
88      /**
89       * @return the length of the script source
90       */
91      int length() {
92          return str.length();
93      }
94  
95      @Override
96      public String toString() {
97          return str;
98      }
99  
100 }