com.squarespace.cldrengine.calendars.DatePatternMatcher Maven / Gradle / Ivy
The newest version!
package com.squarespace.cldrengine.calendars;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.squarespace.cldrengine.calendars.DateSkeleton.SkeletonField;
import com.squarespace.cldrengine.calendars.SkeletonData.Field;
import com.squarespace.cldrengine.calendars.SkeletonData.FieldType;
import com.squarespace.cldrengine.parsing.DateTimePattern;
import com.squarespace.cldrengine.parsing.DateTimePattern.DateTimeNode;
/**
* Matches a skeleton against available patterns.
*/
class DatePatternMatcher {
// Save some work for exact matches.
private final Map EXACT = new HashMap<>();
// Array for matching by distances
private final List ENTRIES = new ArrayList<>();
public void add(DateSkeleton skeleton, DateTimeNode[] pattern) {
String key = skeleton.skeleton;
// Avoid adding patterns with duplicate skeletons
if (!this.EXACT.containsKey(key)) {
this.EXACT.put(key, skeleton);
this.ENTRIES.add(skeleton);
}
// Sort by length so we scan the shorter skeletons first
this.ENTRIES.sort((a, b) -> Integer.compare(a.skeleton.length(), b.skeleton.length()));
}
public DateSkeleton match(DateSkeleton input) {
DateSkeleton match = this.EXACT.get(input.skeleton);
if (match != null) {
return match;
}
DateSkeleton best = null;
int bestDist = Integer.MAX_VALUE;
for (DateSkeleton entry : this.ENTRIES) {
int dist = this.getDistance(entry, input, 0);
if (dist < bestDist) {
best = entry;
bestDist = dist;
if (dist == 0) {
break;
}
}
}
return best;
}
public DateTimePattern adjust(DateTimePattern pattern, DateSkeleton skeleton, String decimal) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy