| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Index |
|
| 1.2941176470588236;1.294 |
| 1 | package org.apache.torque.engine.database.model; | |
| 2 | ||
| 3 | /* | |
| 4 | * Licensed to the Apache Software Foundation (ASF) under one | |
| 5 | * or more contributor license agreements. See the NOTICE file | |
| 6 | * distributed with this work for additional information | |
| 7 | * regarding copyright ownership. The ASF licenses this file | |
| 8 | * to you under the Apache License, Version 2.0 (the | |
| 9 | * "License"); you may not use this file except in compliance | |
| 10 | * with the License. You may obtain a copy of the License at | |
| 11 | * | |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 13 | * | |
| 14 | * Unless required by applicable law or agreed to in writing, | |
| 15 | * software distributed under the License is distributed on an | |
| 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| 17 | * KIND, either express or implied. See the License for the | |
| 18 | * specific language governing permissions and limitations | |
| 19 | * under the License. | |
| 20 | */ | |
| 21 | ||
| 22 | import java.util.ArrayList; | |
| 23 | import java.util.Collections; | |
| 24 | import java.util.Iterator; | |
| 25 | import java.util.List; | |
| 26 | import java.util.Map; | |
| 27 | ||
| 28 | import org.apache.commons.collections.map.ListOrderedMap; | |
| 29 | import org.apache.commons.logging.Log; | |
| 30 | import org.apache.commons.logging.LogFactory; | |
| 31 | ||
| 32 | import org.apache.torque.engine.EngineException; | |
| 33 | ||
| 34 | import org.xml.sax.Attributes; | |
| 35 | ||
| 36 | /** | |
| 37 | * Information about indices of a table. | |
| 38 | * | |
| 39 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> | |
| 40 | * @author <a href="mailto:dlr@finemaltcoding.com>Daniel Rall</a> | |
| 41 | * @author <a href="mailto:monroe@dukece.com>Greg Monroe</a> | |
| 42 | * @version $Id: Index.java,v 1.1 2007-10-21 07:57:27 abyrne Exp $ | |
| 43 | */ | |
| 44 | public class Index | |
| 45 | { | |
| 46 | /** Logging class from commons.logging */ | |
| 47 | 0 | private static Log log = LogFactory.getLog(Index.class); |
| 48 | /** name of the index */ | |
| 49 | private String indexName; | |
| 50 | /** table */ | |
| 51 | private Table parentTable; | |
| 52 | /** columns */ | |
| 53 | private List indexColumns; | |
| 54 | /** The XML Options specified for this index */ | |
| 55 | private Map options; | |
| 56 | ||
| 57 | ||
| 58 | /** | |
| 59 | * Creates a new instance with default characteristics (no name or | |
| 60 | * parent table, small column list size allocation, non-unique). | |
| 61 | */ | |
| 62 | public Index() | |
| 63 | 0 | { |
| 64 | 0 | indexColumns = new ArrayList(3); |
| 65 | 0 | options = Collections.synchronizedMap(new ListOrderedMap()); |
| 66 | 0 | } |
| 67 | ||
| 68 | /** | |
| 69 | * Creates a new instance for the list of columns composing an | |
| 70 | * index. Otherwise performs as {@link #Index()}. | |
| 71 | * | |
| 72 | * @param table The table this index is associated with. | |
| 73 | * @param indexColumns The list of {@link | |
| 74 | * org.apache.torque.engine.database.model.Column} objects which | |
| 75 | * make up this index. Cannot be empty. | |
| 76 | * @exception EngineException Error generating name. | |
| 77 | * @see #Index() | |
| 78 | */ | |
| 79 | protected Index(Table table, List indexColumns) | |
| 80 | throws EngineException | |
| 81 | { | |
| 82 | 0 | this(); |
| 83 | 0 | setTable(table); |
| 84 | 0 | if (!indexColumns.isEmpty()) |
| 85 | { | |
| 86 | 0 | this.indexColumns = indexColumns; |
| 87 | ||
| 88 | 0 | if (log.isDebugEnabled()) |
| 89 | { | |
| 90 | 0 | log.debug("Created Index named " + getName() |
| 91 | + " with " + indexColumns.size() + " columns"); | |
| 92 | } | |
| 93 | } | |
| 94 | else | |
| 95 | { | |
| 96 | 0 | throw new EngineException("Cannot create a new Index using an " |
| 97 | + "empty list Column object"); | |
| 98 | } | |
| 99 | 0 | } |
| 100 | ||
| 101 | /** | |
| 102 | * Imports index from an XML specification | |
| 103 | * | |
| 104 | * @param attrib the xml attributes | |
| 105 | */ | |
| 106 | public void loadFromXML(Attributes attrib) | |
| 107 | { | |
| 108 | 0 | indexName = attrib.getValue("name"); |
| 109 | 0 | } |
| 110 | ||
| 111 | /** | |
| 112 | * Returns the uniqueness of this index. | |
| 113 | * | |
| 114 | * @return the uniqueness of this index | |
| 115 | */ | |
| 116 | public boolean isUnique() | |
| 117 | { | |
| 118 | 0 | return false; |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Gets the name of this index. | |
| 123 | * | |
| 124 | * @return the name of this index | |
| 125 | */ | |
| 126 | public String getName() | |
| 127 | { | |
| 128 | 0 | return indexName; |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * Set the name of this index. | |
| 133 | * | |
| 134 | * @param name the name of this index | |
| 135 | */ | |
| 136 | public void setName(String name) | |
| 137 | { | |
| 138 | 0 | this.indexName = name; |
| 139 | 0 | } |
| 140 | ||
| 141 | /** | |
| 142 | * Set the parent Table of the index | |
| 143 | * | |
| 144 | * @param parent the table | |
| 145 | */ | |
| 146 | public void setTable(Table parent) | |
| 147 | { | |
| 148 | 0 | parentTable = parent; |
| 149 | 0 | } |
| 150 | ||
| 151 | /** | |
| 152 | * Get the parent Table of the index | |
| 153 | * | |
| 154 | * @return the table | |
| 155 | */ | |
| 156 | public Table getTable() | |
| 157 | { | |
| 158 | 0 | return parentTable; |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * Returns the Name of the table the index is in | |
| 163 | * | |
| 164 | * @return the name of the table | |
| 165 | */ | |
| 166 | public String getTableName() | |
| 167 | { | |
| 168 | 0 | return parentTable.getName(); |
| 169 | } | |
| 170 | ||
| 171 | /** | |
| 172 | * Adds a new column to an index. | |
| 173 | * | |
| 174 | * @param attrib xml attributes for the column | |
| 175 | */ | |
| 176 | public void addColumn(Attributes attrib) | |
| 177 | { | |
| 178 | 0 | indexColumns.add(attrib.getValue("name")); |
| 179 | 0 | } |
| 180 | ||
| 181 | /** | |
| 182 | * Return a comma delimited string of the columns which compose this index. | |
| 183 | * | |
| 184 | * @return a list of column names | |
| 185 | */ | |
| 186 | public String getColumnList() | |
| 187 | { | |
| 188 | 0 | return Column.makeList(getColumns()); |
| 189 | } | |
| 190 | ||
| 191 | /** | |
| 192 | * Return the list of local columns. You should not edit this list. | |
| 193 | * | |
| 194 | * @return a list of columns | |
| 195 | */ | |
| 196 | public List getColumns() | |
| 197 | { | |
| 198 | 0 | return indexColumns; |
| 199 | } | |
| 200 | ||
| 201 | /** | |
| 202 | * Returns the list of names of the columns referenced by this | |
| 203 | * index. Slightly over-allocates the list's buffer (just in case | |
| 204 | * more elements are going to be added, such as when a name is | |
| 205 | * being generated). Feel free to modify this list. | |
| 206 | * | |
| 207 | * @return a list of column names | |
| 208 | */ | |
| 209 | protected List getColumnNames() | |
| 210 | { | |
| 211 | 0 | List names = new ArrayList(indexColumns.size() + 2); |
| 212 | 0 | Iterator i = getColumns().iterator(); |
| 213 | 0 | while (i.hasNext()) |
| 214 | { | |
| 215 | 0 | Column c = (Column) i.next(); |
| 216 | 0 | names.add(c.getName()); |
| 217 | 0 | } |
| 218 | 0 | return names; |
| 219 | } | |
| 220 | ||
| 221 | /** | |
| 222 | * String representation of the index. This is an xml representation. | |
| 223 | * | |
| 224 | * @return a xml representation | |
| 225 | */ | |
| 226 | public String toString() | |
| 227 | { | |
| 228 | 0 | StringBuffer result = new StringBuffer(); |
| 229 | 0 | result.append(" <index name=\"") |
| 230 | .append(getName()) | |
| 231 | .append("\""); | |
| 232 | ||
| 233 | 0 | result.append(">\n"); |
| 234 | ||
| 235 | 0 | for (int i = 0; i < indexColumns.size(); i++) |
| 236 | { | |
| 237 | 0 | result.append(" <index-column name=\"") |
| 238 | .append(indexColumns.get(i)) | |
| 239 | .append("\"/>\n"); | |
| 240 | } | |
| 241 | 0 | result.append(" </index>\n"); |
| 242 | 0 | return result.toString(); |
| 243 | } | |
| 244 | ||
| 245 | /** | |
| 246 | * Add an XML Specified option key/value pair to this element's option set. | |
| 247 | * | |
| 248 | * @param key the key of the option. | |
| 249 | * @param value the value of the option. | |
| 250 | */ | |
| 251 | public void addOption(String key, String value) | |
| 252 | { | |
| 253 | 0 | options.put(key, value); |
| 254 | 0 | } |
| 255 | ||
| 256 | /** | |
| 257 | * Get the value that was associated with this key in an XML option | |
| 258 | * element. | |
| 259 | * | |
| 260 | * @param key the key of the option. | |
| 261 | * @return The value for the key or a null. | |
| 262 | */ | |
| 263 | public String getOption(String key) | |
| 264 | { | |
| 265 | 0 | return (String) options.get(key); |
| 266 | } | |
| 267 | ||
| 268 | /** | |
| 269 | * Gets the full ordered hashtable array of items specified by XML option | |
| 270 | * statements under this element.<p> | |
| 271 | * | |
| 272 | * Note, this is not thread save but since it's only used for | |
| 273 | * generation which is single threaded, there should be minimum | |
| 274 | * danger using this in Velocity. | |
| 275 | * | |
| 276 | * @return An Map of all options. Will not be null but may be empty. | |
| 277 | */ | |
| 278 | public Map getOptions() | |
| 279 | { | |
| 280 | 0 | return options; |
| 281 | } | |
| 282 | } |