1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.graph.query.model;
25
26 import net.jcip.annotations.Immutable;
27 import org.modeshape.common.util.CheckArg;
28 import org.modeshape.common.util.ObjectUtil;
29
30 /**
31 *
32 */
33 @Immutable
34 public class Column implements LanguageObject {
35 private static final long serialVersionUID = 1L;
36
37 private final SelectorName selectorName;
38 private final String propertyName;
39 private final String columnName;
40
41 /**
42 * Include a column for each of the single-valued, accessible properties on the node identified by the selector.
43 *
44 * @param selectorName the selector name
45 */
46 public Column( SelectorName selectorName ) {
47 CheckArg.isNotNull(selectorName, "selectorName");
48 this.selectorName = selectorName;
49 this.propertyName = null;
50 this.columnName = null;
51 }
52
53 /**
54 * A column with the given name representing the named property on the node identified by the selector.
55 *
56 * @param selectorName the selector name
57 * @param propertyName the name of the property
58 * @param columnName the name of the column
59 */
60 public Column( SelectorName selectorName,
61 String propertyName,
62 String columnName ) {
63 CheckArg.isNotNull(selectorName, "selectorName");
64 CheckArg.isNotNull(propertyName, "propertyName");
65 CheckArg.isNotNull(columnName, "columnName");
66 this.selectorName = selectorName;
67 this.propertyName = propertyName;
68 this.columnName = columnName;
69 }
70
71 /**
72 * Get the name of the selector for the node.
73 *
74 * @return the selector name; never null
75 */
76 public final SelectorName getSelectorName() {
77 return selectorName;
78 }
79
80 /**
81 * Get the name of the property that this column represents.
82 *
83 * @return the property name; or null if this represents all selectable columns on the {@link #getSelectorName() selector}
84 */
85 public final String getPropertyName() {
86 return propertyName;
87 }
88
89 /**
90 * Get the name of the column.
91 *
92 * @return the column name; or null if this represents all selectable columsn on the {@link #getSelectorName() selector}
93 */
94 public final String getColumnName() {
95 return columnName;
96 }
97
98 /**
99 * {@inheritDoc}
100 *
101 * @see java.lang.Object#toString()
102 */
103 @Override
104 public String toString() {
105 return Visitors.readable(this);
106 }
107
108 /**
109 * {@inheritDoc}
110 *
111 * @see java.lang.Object#hashCode()
112 */
113 @Override
114 public int hashCode() {
115 return getSelectorName().hashCode();
116 }
117
118 /**
119 * {@inheritDoc}
120 *
121 * @see java.lang.Object#equals(java.lang.Object)
122 */
123 @Override
124 public boolean equals( Object obj ) {
125 if (obj == this) return true;
126 if (obj instanceof Column) {
127 Column that = (Column)obj;
128 if (!this.selectorName.equals(that.selectorName)) return false;
129 if (!ObjectUtil.isEqualWithNulls(this.propertyName, that.propertyName)) return false;
130 if (!ObjectUtil.isEqualWithNulls(this.columnName, that.columnName)) return false;
131 return true;
132 }
133 return false;
134 }
135
136 /**
137 * Create a copy of this Column except that uses the supplied selector name instead.
138 *
139 * @param newSelectorName the new selector name
140 * @return a new Column with the supplied selector name and the property and column names from this object; never null
141 * @throws IllegalArgumentException if the supplied selector name is null
142 */
143 public Column with( SelectorName newSelectorName ) {
144 return new Column(newSelectorName, propertyName, columnName);
145 }
146
147 /**
148 * {@inheritDoc}
149 *
150 * @see org.modeshape.graph.query.model.Visitable#accept(org.modeshape.graph.query.model.Visitor)
151 */
152 public void accept( Visitor visitor ) {
153 visitor.visit(this);
154 }
155 }