1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.properties.api.core.domain; 17 18 import de.smartics.util.lang.Arg; 19 import de.smartics.util.lang.BlankArgumentException; 20 21 /** 22 * Contains information about the source the property is defined in. 23 */ 24 public final class SourceInfo 25 { 26 // ********************************* Fields ********************************* 27 28 // --- constants ------------------------------------------------------------ 29 30 // --- members -------------------------------------------------------------- 31 32 /** 33 * Reference to the empty source information. 34 */ 35 public static final SourceInfo EMPTY = new SourceInfo(); 36 37 /** 38 * The name of the element's type. 39 */ 40 private final String elementTypeName; 41 42 /** 43 * The name of the element that defines the property. 44 */ 45 private final String elementName; 46 47 /** 48 * The line number where the definition of the property begins. 49 */ 50 private final int lineNumber; 51 52 // ****************************** Initializer ******************************* 53 54 // ****************************** Constructors ****************************** 55 56 private SourceInfo() 57 { 58 this("n/a", "n/a", -1); 59 } 60 61 /** 62 * Default constructor. 63 * 64 * @param elementTypeName the name of the element's type. 65 * @param elementName the name of the element that defines the property. 66 * @param lineNumber the line number where the definition of the property 67 * begins. 68 * @throws BlankArgumentException if either {@code elementTypeName} or 69 * {@code elementName} is blank. 70 */ 71 public SourceInfo(final String elementTypeName, final String elementName, 72 final int lineNumber) throws BlankArgumentException 73 { 74 this.elementTypeName = 75 Arg.checkNotBlank("elementTypeName", elementTypeName); 76 this.elementName = Arg.checkNotBlank("elementName", elementName); 77 this.lineNumber = lineNumber; 78 } 79 80 /** 81 * Convenience constructor to parse the line number. 82 * 83 * @param elementTypeName the name of the element's type. 84 * @param elementName the name of the element that defines the property. 85 * @param lineNumber the line number where the definition of the property 86 * begins. If this cannot be parsed as an integer number, the value 87 * is set to <code>-1</code>. 88 * @throws BlankArgumentException if either {@code elementTypeName} or 89 * {@code elementName} is blank. 90 */ 91 public SourceInfo(final String elementTypeName, final String elementName, 92 final String lineNumber) throws BlankArgumentException 93 { 94 this(elementTypeName, elementName, parseLineNumber(lineNumber)); 95 } 96 97 // ****************************** Inner Classes ***************************** 98 99 // ********************************* Methods ******************************** 100 101 // --- init ----------------------------------------------------------------- 102 103 private static int parseLineNumber(final String lineNumber) 104 { 105 try 106 { 107 return Integer.parseInt(lineNumber); 108 } 109 catch (final NumberFormatException e) 110 { 111 return -1; 112 } 113 } 114 115 // --- get&set -------------------------------------------------------------- 116 117 /** 118 * Returns the name of the element's type. 119 * 120 * @return the name of the element's type. 121 */ 122 public String getElementTypeName() 123 { 124 return elementTypeName; 125 } 126 127 /** 128 * Returns the name of the element that defines the property. 129 * 130 * @return the name of the element that defines the property. 131 */ 132 public String getElementName() 133 { 134 return elementName; 135 } 136 137 /** 138 * Returns the line number where the definition of the property begins. 139 * 140 * @return the line number where the definition of the property begins. 141 */ 142 public int getLineNumber() 143 { 144 return lineNumber; 145 } 146 147 // --- business ------------------------------------------------------------- 148 149 /** 150 * Returns the element identifier that consists of the type and the element 151 * name. 152 * 153 * @return the unique identifier of the declaring element. 154 */ 155 public String getElementId() 156 { 157 return elementTypeName + '.' + elementName; 158 } 159 160 // --- object basics -------------------------------------------------------- 161 162 /** 163 * Returns the string representation of the object. 164 * 165 * @return the string representation of the object. 166 */ 167 @Override 168 public String toString() 169 { 170 return elementTypeName + '#' + elementName + ':' + lineNumber; 171 } 172 }