PrecompiledTemplateMapKey.java revision 56ed4167b942ec265f9cee70ac4d71d10b3835ce
1/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.clearsilver.jsilver.precompiler;
18
19import com.google.clearsilver.jsilver.autoescape.EscapeMode;
20
21/**
22 * Object to use as key when looking up precompiled templates. It encapsulates the template name, as
23 * well as the {@link EscapeMode} for which the template was compiled.
24 */
25public class PrecompiledTemplateMapKey {
26  private final Object templateName;
27  private final EscapeMode escapeMode;
28  private final String toStringName;
29
30  public PrecompiledTemplateMapKey(Object templateName, EscapeMode escapeMode) {
31    this.templateName = templateName;
32    this.escapeMode = escapeMode;
33
34    if (escapeMode == EscapeMode.ESCAPE_NONE) {
35      toStringName = templateName.toString();
36    } else {
37      toStringName = templateName.toString() + "_" + escapeMode.getEscapeCommand();
38    }
39  }
40
41  public boolean equals(Object o) {
42    if (o == this) {
43      return true;
44    }
45
46    if (o == null || getClass() != o.getClass()) {
47      return false;
48    }
49
50    PrecompiledTemplateMapKey that = (PrecompiledTemplateMapKey) o;
51
52    return templateName.equals(that.templateName) && (escapeMode == that.escapeMode);
53  }
54
55  public int hashCode() {
56    int hash = 17;
57
58    hash = 31 * hash + templateName.hashCode();
59    hash = 31 * hash + escapeMode.hashCode();
60    return hash;
61  }
62
63  /**
64   * String representation of key. If the template was auto escaped, it appends the
65   * {@link EscapeMode} to the template name.
66   *
67   */
68  public String toString() {
69    return toStringName;
70  }
71
72  /**
73   * Return the escape mode used for this template.
74   */
75  public EscapeMode getEscapeMode() {
76    return escapeMode;
77  }
78}
79