View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.context;
21  
22  import org.apache.myfaces.tobago.context.ClientProperties;
23  import org.apache.myfaces.tobago.context.Theme;
24  import org.apache.myfaces.tobago.context.UserAgent;
25  
26  import javax.faces.component.UIViewRoot;
27  import javax.faces.context.FacesContext;
28  import java.io.Serializable;
29  import java.util.Locale;
30  import java.util.Map;
31  
32  public final class ClientPropertiesKey implements Serializable {
33  
34    private static final String KEY_IN_FACES_CONTEXT = ClientPropertiesKey.class.getName();
35    
36    private final String contentType;
37    private final Theme theme;
38    private final UserAgent userAgent;
39    private final Locale locale;
40  
41    private final int hashCode;
42  
43    public static ClientPropertiesKey get(final FacesContext facesContext) {
44      final Map<Object, Object> attributes = facesContext.getAttributes();
45      ClientPropertiesKey key = (ClientPropertiesKey) attributes.get(KEY_IN_FACES_CONTEXT);
46      if (key == null) {
47        final ClientProperties clientProperties = ClientProperties.getInstance(facesContext);
48        key = new ClientPropertiesKey(clientProperties, facesContext.getViewRoot());
49        attributes.put(KEY_IN_FACES_CONTEXT, key);
50      }
51      return key;
52    }
53  
54    public static void reset(final FacesContext facesContext) {
55      final Map<Object, Object> attributes = facesContext.getAttributes();
56      attributes.remove(KEY_IN_FACES_CONTEXT);
57    }
58    
59    private ClientPropertiesKey(final ClientProperties clientProperties, final UIViewRoot viewRoot) {
60      contentType = clientProperties.getContentType();
61      theme = clientProperties.getTheme();
62      userAgent = clientProperties.getUserAgent();
63      locale = viewRoot.getLocale();
64      hashCode = calcHashCode();
65    }
66  
67    @Override
68    public boolean equals(final Object o) {
69      if (this == o) {
70        return true;
71      }
72      if (o == null || getClass() != o.getClass()) {
73        return false;
74      }
75  
76      final ClientPropertiesKey that = (ClientPropertiesKey) o;
77  
78      if (!contentType.equals(that.contentType)) {
79        return false;
80      }
81      if (locale == null) {
82        if (that.locale != null) {
83          return false;
84        }
85      } else {
86        if (!locale.equals(that.locale)) {
87          return false;
88        }
89      }
90      if (!theme.equals(that.theme)) {
91        return false;
92      }
93      if (!userAgent.equals(that.userAgent)) {
94        return false;
95      }
96  
97      return true;
98    }
99  
100   private int calcHashCode() {
101     int result = contentType.hashCode();
102     result = 31 * result + theme.hashCode();
103     result = 31 * result + userAgent.hashCode();
104     if (locale != null) {
105       result = 31 * result + locale.hashCode();
106     }
107     return result;
108   }
109 
110   @Override
111   public int hashCode() {
112     return hashCode;
113   }
114 
115   @Override
116   public String toString() {
117     return "ClientPropertiesKey(" + contentType + "," + theme + "," + userAgent + "," + locale + "," + hashCode + ')';
118   }
119 
120   public String getContentType() {
121     return contentType;
122   }
123 
124   public Theme getTheme() {
125     return theme;
126   }
127 
128   public UserAgent getUserAgent() {
129     return userAgent;
130   }
131 
132   public Locale getLocale() {
133     return locale;
134   }
135 }