| Class | Line # | Actions | |||||
|---|---|---|---|---|---|---|---|
| TypedValue | 30 | 12 | 0% | 11 | 0 |
| 1 | /* | |
| 2 | * See the NOTICE file distributed with this work for additional | |
| 3 | * information regarding copyright ownership. | |
| 4 | * | |
| 5 | * This is free software; you can redistribute it and/or modify it | |
| 6 | * under the terms of the GNU Lesser General Public License as | |
| 7 | * published by the Free Software Foundation; either version 2.1 of | |
| 8 | * the License, or (at your option) any later version. | |
| 9 | * | |
| 10 | * This software is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | * Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU Lesser General Public | |
| 16 | * License along with this software; if not, write to the Free | |
| 17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
| 18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
| 19 | */ | |
| 20 | package org.xwiki.search.solr.internal.metadata; | |
| 21 | ||
| 22 | import java.util.Date; | |
| 23 | ||
| 24 | /** | |
| 25 | * A (value, type) pair. | |
| 26 | * | |
| 27 | * @version $Id: 57656d2a37620ba7f3059ab72374b1f72fff18d3 $ | |
| 28 | * @since 5.3RC1 | |
| 29 | */ | |
| 30 | public final class TypedValue | |
| 31 | { | |
| 32 | /** | |
| 33 | * The string type is used for fields that require exact matching. String fields are stored as is in the index | |
| 34 | * without being analyzed. | |
| 35 | */ | |
| 36 | public static final String STRING = "string"; | |
| 37 | ||
| 38 | /** | |
| 39 | * The text type is used for fields that should be analyzed (split in tokens, strip stop words, etc.). These fields | |
| 40 | * usually contain free text and are indexed in a specific locale. | |
| 41 | */ | |
| 42 | public static final String TEXT = null; | |
| 43 | ||
| 44 | private final Object value; | |
| 45 | ||
| 46 | private final String type; | |
| 47 | ||
| 48 | /** | |
| 49 | * Creates a new (value, type) pair where the type is inferred from the value. | |
| 50 | * | |
| 51 | * @param value the value | |
| 52 | */ | |
| 53 | 8730 | public TypedValue(Object value) |
| 54 | { | |
| 55 | 8730 | this(value, typeOf(value)); |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * Creates a new (value, type) pair. | |
| 60 | * | |
| 61 | * @param value the value | |
| 62 | * @param type the data type | |
| 63 | */ | |
| 64 | 15062 | public TypedValue(Object value, String type) |
| 65 | { | |
| 66 | 15062 | this.value = value; |
| 67 | 15062 | this.type = type; |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * @return the value | |
| 72 | */ | |
| 73 | 19419 | public Object getValue() |
| 74 | { | |
| 75 | 19419 | return value; |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * @return the data type | |
| 80 | */ | |
| 81 | 4357 | public String getType() |
| 82 | { | |
| 83 | 4357 | return type; |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Utility method that can be used to get a suffix to add to a dynamic field name so that its value is indexed | |
| 88 | * properly. | |
| 89 | * | |
| 90 | * @param value the value of a dynamic field | |
| 91 | * @return the corresponding type, as per schema.xml, or {@code null} if the given value doesn't have a type known | |
| 92 | * by schema.xml | |
| 93 | */ | |
| 94 | 8730 | private static String typeOf(Object value) |
| 95 | { | |
| 96 | 8730 | if (value instanceof Integer) { |
| 97 | // We could have grouped Integer with the rest of the final types but we use it's short name in schema.xml | |
| 98 | 4 | return "int"; |
| 99 | 8726 | } else if (value instanceof Date) { |
| 100 | // Date is not final so we use "date" for any of its subclasses. | |
| 101 | 15 | return "date"; |
| 102 | 8711 | } else if (value instanceof Boolean || value instanceof Long || value instanceof Double |
| 103 | || value instanceof Float) { | |
| 104 | // All these types are final so we are safe with using the simple class name. | |
| 105 | 1535 | return value.getClass().getSimpleName().toLowerCase(); |
| 106 | } | |
| 107 | ||
| 108 | // If we don't know the type then we index the value as string to be able to perform exact matches. | |
| 109 | 7176 | return STRING; |
| 110 | } | |
| 111 | } |