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