| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DocumentAttributeFactory |
|
| 2.1;2.1 |
| 1 | /** | |
| 2 | * Copyright 2005-2011 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php | |
| 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 org.kuali.rice.kew.api.document.attribute; | |
| 17 | ||
| 18 | import org.joda.time.DateTime; | |
| 19 | ||
| 20 | import java.math.BigDecimal; | |
| 21 | import java.math.BigInteger; | |
| 22 | import java.util.Calendar; | |
| 23 | import java.util.Date; | |
| 24 | ||
| 25 | /** | |
| 26 | * A factory that helps with creation of new {@link DocumentAttribute} instances as well as translation to concrete | |
| 27 | * instances from a {@link DocumentAttributeContract} | |
| 28 | * | |
| 29 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 30 | */ | |
| 31 | 0 | public class DocumentAttributeFactory { |
| 32 | ||
| 33 | /** | |
| 34 | * Constructs a document attribute containing character data from the given attribute name and {@link String} value. | |
| 35 | * | |
| 36 | * @param name the name of the attribute to construct, must not be a null or blank value | |
| 37 | * @param value the value of the attribute as a {@code String} | |
| 38 | * | |
| 39 | * @return a constructed {@code DocumentAttributeString} representing the document attribute | |
| 40 | * | |
| 41 | * @throws IllegalArgumentException if name is a null or blank value | |
| 42 | */ | |
| 43 | public static DocumentAttributeString createStringAttribute(String name, String value) { | |
| 44 | 0 | DocumentAttributeString.Builder builder = DocumentAttributeString.Builder.create(name); |
| 45 | 0 | builder.setValue(value); |
| 46 | 0 | return builder.build(); |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Constructs a document attribute containing date/time data from the given attribute name and {@link DateTime} | |
| 51 | * object. | |
| 52 | * | |
| 53 | * @param name the name of the attribute to construct, must not be a null or blank value | |
| 54 | * @param value the value of the attribute as a {@code DateTime} | |
| 55 | * | |
| 56 | * @return a constructed {@code DocumentAttributeDateTime} representing the document attribute | |
| 57 | * | |
| 58 | * @throws IllegalArgumentException if name is a null or blank value | |
| 59 | */ | |
| 60 | public static DocumentAttributeDateTime createDateTimeAttribute(String name, DateTime value) { | |
| 61 | 0 | DocumentAttributeDateTime.Builder builder = DocumentAttributeDateTime.Builder.create(name); |
| 62 | 0 | builder.setValue(value); |
| 63 | 0 | return builder.build(); |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Constructs a document attribute containing date/time data from the given attribute name and {@link Date} object. | |
| 68 | * | |
| 69 | * @param name the name of the attribute to construct, must not be a null or blank value | |
| 70 | * @param value the value of the attribute as a {@code Date} | |
| 71 | * | |
| 72 | * @return a constructed {@code DocumentAttributeDateTime} representing the document attribute | |
| 73 | * | |
| 74 | * @throws IllegalArgumentException if name is a null or blank value | |
| 75 | */ | |
| 76 | public static DocumentAttributeDateTime createDateTimeAttribute(String name, Date value) { | |
| 77 | 0 | return createDateTimeAttribute(name, new DateTime(value)); |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * Constructs a document attribute containing date/time data from the given attribute name and a numeric long | |
| 82 | * representing the number of milliseconds from 1970-01-01T00:00:00Z in the default time zone. | |
| 83 | * | |
| 84 | * @param name the name of the attribute to construct, must not be a null or blank value | |
| 85 | * @param instant the instant value represented as milliseconds from 1970-01-01T00:00:00Z | |
| 86 | * | |
| 87 | * @return a constructed {@code DocumentAttributeDateTime} representing the document attribute | |
| 88 | * | |
| 89 | * @throws IllegalArgumentException if name is a null or blank value | |
| 90 | */ | |
| 91 | public static DocumentAttributeDateTime createDateTimeAttribute(String name, long instant) { | |
| 92 | 0 | return createDateTimeAttribute(name, new DateTime(instant)); |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Constructs a document attribute containing date/time data from the given attribute name and {@link Calendar} | |
| 97 | * object. | |
| 98 | * | |
| 99 | * @param name the name of the attribute to construct, must not be a null or blank value | |
| 100 | * @param value the value of the attribute as a {@code Calendar} | |
| 101 | * | |
| 102 | * @return a constructed {@code DocumentAttributeDateTime} representing the document attribute | |
| 103 | * | |
| 104 | * @throws IllegalArgumentException if name is a null or blank value | |
| 105 | */ | |
| 106 | public static DocumentAttributeDateTime createDateTimeAttribute(String name, Calendar value) { | |
| 107 | 0 | return createDateTimeAttribute(name, new DateTime(value)); |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * Constructs a document attribute containing real number data from the given attribute name and {@link BigDecimal} | |
| 112 | * object. | |
| 113 | * | |
| 114 | * @param name the name of the attribute to construct, must not be a null or blank value | |
| 115 | * @param value the value of the attribute as a {@code BigDecimal} | |
| 116 | * | |
| 117 | * @return a constructed {@code DocumentAttributeDecimal} representing the document attribute | |
| 118 | * | |
| 119 | * @throws IllegalArgumentException if name is a null or blank value | |
| 120 | */ | |
| 121 | public static DocumentAttributeDecimal createDecimalAttribute(String name, BigDecimal value) { | |
| 122 | 0 | DocumentAttributeDecimal.Builder builder = DocumentAttributeDecimal.Builder.create(name); |
| 123 | 0 | builder.setValue(value); |
| 124 | 0 | return builder.build(); |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * Constructs a document attribute containing real number data from the given attribute name and {@link Number} | |
| 129 | * object. The given number is first translated to a {@code BigDecimal} using {@link Number#doubleValue()}. | |
| 130 | * | |
| 131 | * @param name the name of the attribute to construct, must not be a null or blank value | |
| 132 | * @param value the value of the attribute as a {@code Number} | |
| 133 | * | |
| 134 | * @return a constructed {@code DocumentAttributeDecimal} representing the document attribute | |
| 135 | * | |
| 136 | * @throws IllegalArgumentException if name is a null or blank value | |
| 137 | */ | |
| 138 | public static DocumentAttributeDecimal createDecimalAttribute(String name, Number value) { | |
| 139 | 0 | return createDecimalAttribute(name, BigDecimal.valueOf(value.doubleValue())); |
| 140 | } | |
| 141 | ||
| 142 | /** | |
| 143 | * Constructs a document attribute containing integer number data from the given attribute name and | |
| 144 | * {@link BigInteger} object. | |
| 145 | * | |
| 146 | * @param name the name of the attribute to construct, must not be a null or blank value | |
| 147 | * @param value the value of the attribute as a {@code BigInteger} | |
| 148 | * | |
| 149 | * @return a constructed {@code DocumentAttributeInteger} representing the document attribute | |
| 150 | * | |
| 151 | * @throws IllegalArgumentException if name is a null or blank value | |
| 152 | */ | |
| 153 | public static DocumentAttributeInteger createIntegerAttribute(String name, BigInteger value) { | |
| 154 | 0 | DocumentAttributeInteger.Builder builder = DocumentAttributeInteger.Builder.create(name); |
| 155 | 0 | builder.setValue(value); |
| 156 | 0 | return builder.build(); |
| 157 | } | |
| 158 | ||
| 159 | /** | |
| 160 | * Constructs a document attribute containing integer number data from the given attribute name and {@link Number} | |
| 161 | * object. The given number is first translated to a {@code BigInteger} using {@link Number#longValue()}. | |
| 162 | * | |
| 163 | * @param name the name of the attribute to construct, must not be a null or blank value | |
| 164 | * @param value the value of the attribute as a {@code Number} | |
| 165 | * | |
| 166 | * @return a constructed {@code DocumentAttributeInteger} representing the document attribute | |
| 167 | * | |
| 168 | * @throws IllegalArgumentException if name is a null or blank value | |
| 169 | */ | |
| 170 | public static DocumentAttributeInteger createIntegerAttribute(String name, Number value) { | |
| 171 | 0 | return createIntegerAttribute(name, BigInteger.valueOf(value.longValue())); |
| 172 | } | |
| 173 | ||
| 174 | /** | |
| 175 | * Loads the given {@link DocumentAttributeContract} into the appropriate builder instance based on the type of the | |
| 176 | * given contract implementation. | |
| 177 | * | |
| 178 | * @param contract the contract to load into a builder | |
| 179 | * @return an implementation of {@link DocumentAttribute.AbstractBuilder} which handles instance of the given | |
| 180 | * contract | |
| 181 | * | |
| 182 | * @throws IllegalArgumentException if the given contract is null | |
| 183 | * @throws IllegalArgumentException if a builder implementation could not be determined into which to load the given | |
| 184 | * contract implementation | |
| 185 | */ | |
| 186 | public static DocumentAttribute.AbstractBuilder<?> loadContractIntoBuilder(DocumentAttributeContract contract) { | |
| 187 | 0 | if (contract == null) { |
| 188 | 0 | throw new IllegalArgumentException("contract was null"); |
| 189 | } | |
| 190 | 0 | if (contract instanceof DocumentAttributeString) { |
| 191 | 0 | DocumentAttributeString attribute = (DocumentAttributeString)contract; |
| 192 | 0 | DocumentAttributeString.Builder builder = DocumentAttributeString.Builder.create(attribute.getName()); |
| 193 | 0 | builder.setValue(attribute.getValue()); |
| 194 | 0 | return builder; |
| 195 | 0 | } else if (contract instanceof DocumentAttributeDateTime) { |
| 196 | 0 | DocumentAttributeDateTime attribute = (DocumentAttributeDateTime)contract; |
| 197 | 0 | DocumentAttributeDateTime.Builder builder = DocumentAttributeDateTime.Builder.create(attribute.getName()); |
| 198 | 0 | builder.setValue(attribute.getValue()); |
| 199 | 0 | return builder; |
| 200 | 0 | } else if (contract instanceof DocumentAttributeInteger) { |
| 201 | 0 | DocumentAttributeInteger attribute = (DocumentAttributeInteger)contract; |
| 202 | 0 | DocumentAttributeInteger.Builder builder = DocumentAttributeInteger.Builder.create(attribute.getName()); |
| 203 | 0 | builder.setValue(attribute.getValue()); |
| 204 | 0 | return builder; |
| 205 | 0 | } else if (contract instanceof DocumentAttributeDecimal) { |
| 206 | 0 | DocumentAttributeDecimal attribute = (DocumentAttributeDecimal)contract; |
| 207 | 0 | DocumentAttributeDecimal.Builder builder = DocumentAttributeDecimal.Builder.create(attribute.getName()); |
| 208 | 0 | builder.setValue(attribute.getValue()); |
| 209 | 0 | return builder; |
| 210 | } | |
| 211 | 0 | throw new IllegalArgumentException("Given document attribute class could not be converted: " + contract.getClass()); |
| 212 | } | |
| 213 | ||
| 214 | ||
| 215 | } |