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 org.apache.commons.jexl3.JexlFeatures;
20  
21  import java.util.Objects;
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 {
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      /**
51       * @return the length of the script source
52       */
53      int length() {
54          return str.length();
55      }
56  
57      @Override
58      public int hashCode() {
59          return hashCode;
60      }
61  
62      @Override
63      public boolean equals(final Object obj) {
64          if (this == obj) {
65              return true;
66          }
67          if (obj == null) {
68              return false;
69          }
70          if (getClass() != obj.getClass()) {
71              return false;
72          }
73          final Source other = (Source) obj;
74          if (!Objects.equals(this.features, other.features)) {
75              return false;
76          }
77          if (!Objects.equals(this.str, other.str)) {
78              return false;
79          }
80          return true;
81      }
82  
83      @Override
84      public String toString() {
85          return str;
86      }
87  
88      /**
89       * @return the features associated with the source
90       */
91      public JexlFeatures getFeatures() {
92          return features;
93      }
94  
95  }