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 package org.apache.myfaces.trinidad.style;
20
21 /**
22 * A Selector object holds a CSS selector. To create a new Selector, call the
23 * static method Selector.createSelector(String selectorString).
24 * This class makes the Styles Object APIs clearer,
25 * since we have Map<Selector, Style> now instead of Map<String, Style>.
26 * Also with this object we'll have the API in place in case we need to
27 * hang methods off of this object (like
28 * possibily reordering the pseudo-classes in alphabetical order when creating
29 * a Selector object so that af|foo:bar:zoo and af|foo:zoo:bar are equal).
30 * It was originally thought that we'd add a getNativeSelectorString method
31 * here, but we decided to not add it here to keep a better separation of Selectors
32 * and the maps that convert the Selectors to the native selector string.
33 * @see Styles#getNativeSelectorString(org.apache.myfaces.trinidad.style.Selector) ;
34 */
35 final public class Selector
36 {
37
38 /**
39 * Given a String that represents the selector, return a Selector object
40 * @param selectorString
41 * @return a Selector object
42 * @throws IllegalArgumentException if selectorString is null or the empty String.
43 */
44 public static Selector createSelector(String selectorString)
45 {
46 if (selectorString == null || selectorString.equals(""))
47 throw new IllegalArgumentException("selectorString must be non null and non empty.");
48
49 return new Selector(selectorString);
50 }
51
52 // toString
53 @Override
54 public String toString()
55 {
56 return _selectorString;
57 }
58
59 @Override
60 public int hashCode()
61 {
62 // delegate to the String's hashCode();
63 return _selectorString.hashCode();
64 }
65
66 @Override
67 public boolean equals(Object obj)
68 {
69 if (this == obj)
70 return true;
71 if (obj == null || !(obj instanceof Selector))
72 return false;
73
74 // obj at this point must be a Selector
75 Selector test = (Selector)obj;
76
77 return (_selectorString.equals(test._selectorString));
78 }
79
80
81 // Do not call directly. Call from Selector.createSelector
82 private Selector(String selectorString)
83 {
84 _selectorString = selectorString;
85 }
86
87
88 private final String _selectorString;
89
90
91 }