// -----------------------------------------------------------------------
//
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// -----------------------------------------------------------------------
namespace Lucene.Net.Analysis
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
///
/// A subclass of for the purpose of making it easier to implement
/// re-use.
///
///
///
/// is meant to support easy re-use of
/// for the most common use-cases. Analyzers like PerFieldAnalyzerWraper that behave
/// differently depending upon the field name may need to subclass
/// directly.
///
/// Subclasses must implement .
///
/// For consistency, this class does not allow subclasses to extend
/// or
/// directly.
///
///
public abstract class ReusableAnalyzerBase : Analyzer
{
///
/// Finds or creates a that is permits the
/// to be re-used on the same thread.
///
///
///
/// The overridden behavior of this method is to check
/// to see if a object is already stored there. If not, it creates
/// a new instance of and stores it in .
/// The held inside the current the
/// instance is then returned.
///
///
/// Name of the field.
/// The reader.
///
/// An instance of .
///
public sealed override TokenStream ReusableTokenStream(string fieldName, StreamReader reader)
{
var components = this.PreviousTokenStreamOrStorage as TokenStreamComponents;
var initializedReader = this.InitializeReader(reader);
if (components == null || !components.Reset(initializedReader))
{
components = this.CreateComponents(fieldName, initializedReader);
this.PreviousTokenStreamOrStorage = components;
}
return components.TokenStream;
}
///
/// Creates a using the specified .
///
/// Name of the field.
/// The reader.
///
/// An instance of .
///
///
/// Subclasses that implement this method should always be able to handle null
/// values for the field name for backwards compatibility.
///
public sealed override TokenStream TokenStream(string fieldName, StreamReader reader)
{
var initializedReader = this.InitializeReader(reader);
var components = this.CreateComponents(fieldName, initializedReader);
return components.TokenStream;
}
///
/// Creates a new instance of .
///
/// Name of the file.
/// The reader.
///
/// An instance of .
///
protected abstract TokenStreamComponents CreateComponents(string fieldName, StreamReader reader);
///
/// Initializes the .
///
/// The reader.
///
/// An instance of .
///
protected virtual StreamReader InitializeReader(StreamReader reader)
{
return reader;
}
///
/// The components of a . This class
/// provides access to the source and the outer end.
/// The outer end is instance of TokenFilter which is also a .
///
protected internal class TokenStreamComponents
{
private readonly Tokenizer tokenizer;
private readonly TokenStream tokenStream;
///
/// Initializes a new instance of the class.
///
/// The tokenizer.
public TokenStreamComponents(Tokenizer tokenizer)
: this(tokenizer, tokenizer)
{
}
///
/// Initializes a new instance of the class.
///
/// The tokenizer.
/// The token stream.
public TokenStreamComponents(Tokenizer tokenizer, TokenStream tokenStream)
{
this.tokenizer = tokenizer;
this.tokenStream = tokenStream;
}
///
/// Gets the token stream.
///
/// The token stream.
protected internal TokenStream TokenStream
{
get { return this.tokenStream; }
}
///
/// Resets the components with the specified .
///
/// The reader.
/// true if the internal components where reset, otherwise false.
///
/// Thrown when the internal throws an
/// /
///
protected internal bool Reset(StreamReader reader)
{
this.tokenizer.Reset(reader);
return true;
}
}
}
}