All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.search.bridge.builtin.StringEncodingDateBridge Maven / Gradle / Ivy

There is a newer version: 5.11.12.Final
Show newest version
/*
 * Hibernate Search, full-text search for your domain model
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.search.bridge.builtin;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexableField;
import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.bridge.ParameterizedBridge;
import org.hibernate.search.bridge.TwoWayFieldBridge;
import org.hibernate.search.bridge.builtin.impl.DateResolutionUtil;
import org.hibernate.search.bridge.spi.IgnoreAnalyzerBridge;
import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;
import java.lang.invoke.MethodHandles;

/**
 * Bridge a {@code java.util.Date} to a {@code String}, truncated to the specified resolution.
 * GMT is used as time zone.
 * 
    *
  • Resolution.YEAR: yyyy
  • *
  • Resolution.MONTH: yyyyMM
  • *
  • Resolution.DAY: yyyyMMdd
  • *
  • Resolution.HOUR: yyyyMMddHH
  • *
  • Resolution.MINUTE: yyyyMMddHHmm
  • *
  • Resolution.SECOND: yyyyMMddHHmmss
  • *
  • Resolution.MILLISECOND: yyyyMMddHHmmssSSS
  • *
* * @author Emmanuel Bernard */ public class StringEncodingDateBridge implements TwoWayFieldBridge, ParameterizedBridge, IgnoreAnalyzerBridge { private static final Log log = LoggerFactory.make( MethodHandles.lookup() ); public static final TwoWayFieldBridge DATE_YEAR = new StringEncodingDateBridge( Resolution.YEAR ); public static final TwoWayFieldBridge DATE_MONTH = new StringEncodingDateBridge( Resolution.MONTH ); public static final TwoWayFieldBridge DATE_DAY = new StringEncodingDateBridge( Resolution.DAY ); public static final TwoWayFieldBridge DATE_HOUR = new StringEncodingDateBridge( Resolution.HOUR ); public static final TwoWayFieldBridge DATE_MINUTE = new StringEncodingDateBridge( Resolution.MINUTE ); public static final TwoWayFieldBridge DATE_SECOND = new StringEncodingDateBridge( Resolution.SECOND ); public static final TwoWayFieldBridge DATE_MILLISECOND = new StringEncodingDateBridge( Resolution.MILLISECOND ); private DateTools.Resolution resolution; public StringEncodingDateBridge() { } public StringEncodingDateBridge(Resolution resolution) { this.resolution = DateResolutionUtil.getLuceneResolution( resolution ); } @Override public Object get(String name, Document document) { final IndexableField field = document.getField( name ); if ( field != null ) { try { return DateTools.stringToDate( field.stringValue() ); } catch (ParseException e) { throw log.invalidStringDateFieldInDocument( name, field.stringValue() ); } } else { return null; } } @Override public String objectToString(Object object) { return object != null ? DateTools.dateToString( (Date) object, resolution ) : null; } @Override public void set(String name, Object value, Document document, LuceneOptions luceneOptions) { if ( value == null ) { return; } Date date = (Date) value; String stringDate = DateTools.dateToString( date, resolution ); luceneOptions.addFieldToDocument( name, stringDate, document ); } @Override public void setParameterValues(Map parameters) { String resolution = parameters.get( "resolution" ); Resolution hibResolution = Resolution.valueOf( resolution.toUpperCase( Locale.ENGLISH ) ); this.resolution = DateResolutionUtil.getLuceneResolution( hibResolution ); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy