com.github.liblevenshtein.distance.factory.MemoizedDistanceFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liblevenshtein-lite Show documentation
Show all versions of liblevenshtein-lite Show documentation
A library for spelling-correction based on Levenshtein Automata.
The newest version!
package com.github.liblevenshtein.distance.factory;
import com.github.liblevenshtein.distance.IDistance;
import com.github.liblevenshtein.distance.MemoizedMergeAndSplit;
import com.github.liblevenshtein.distance.MemoizedStandard;
import com.github.liblevenshtein.distance.MemoizedTransposition;
import com.github.liblevenshtein.transducer.Algorithm;
import java.io.Serializable;
/**
* Builds memoized instances of Levenshtein distance metrics.
* @author Dylon Edwards
* @since 2.1.0
*/
public class MemoizedDistanceFactory implements IDistanceFactory, Serializable {
private static final long serialVersionUID = 1L;
/**
* Computes the distance between two terms using the standard, Levenshtein
* distance algorithm.
*/
private volatile IDistance standard = null;
/**
* Computes the distance between two terms using the standard, Levenshtein
* distance algorithm extended with transpositions.
*/
private volatile IDistance transposition = null;
/**
* Computes the distance between two terms using the standard, Levenshtein
* distance algorithm extended with merges and splits.
*/
private volatile IDistance mergeAndSplit = null;
/**
* {@inheritDoc}
*/
@Override
public IDistance build(final Algorithm algorithm) {
switch (algorithm) {
case STANDARD:
if (null == standard) {
synchronized (this) {
if (null == standard) {
standard = new MemoizedStandard();
}
}
}
return standard;
case TRANSPOSITION:
if (null == transposition) {
synchronized (this) {
if (null == transposition) {
transposition = new MemoizedTransposition();
}
}
}
return transposition;
case MERGE_AND_SPLIT:
if (null == mergeAndSplit) {
synchronized (this) {
if (null == mergeAndSplit) {
mergeAndSplit = new MemoizedMergeAndSplit();
}
}
}
return mergeAndSplit;
default:
throw new IllegalArgumentException("Unrecognized algorithm: " + algorithm);
}
}
}