001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.outerj.daisy.repository.serverimpl.query.facets;
017:
018: import java.util.Comparator;
019: import java.util.Map;
020: import java.util.SortedMap;
021:
022: import org.outerj.daisy.query.model.ValueExpr;
023: import org.outerj.daisy.repository.serverimpl.query.Formatter;
024:
025: public class CountGroup {
026: private Object min;
027:
028: private Object max;
029:
030: private long count;
031:
032: private Comparator<Object> comparator;
033:
034: private AbstractFacetSampler facetSampler;
035:
036: private ValueExpr valueExpr;
037:
038: private Map<String, Formatter> formatters;
039:
040: private String userValue;
041:
042: private String queryValue;
043:
044: public CountGroup(AbstractFacetSampler facetSampler, Object o,
045: long count, ValueExpr valueExpr,
046: Map<String, Formatter> formatters) {
047: this .count = count;
048: min = o;
049: max = o;
050: comparator = facetSampler.getComparator();
051: this .facetSampler = facetSampler;
052: this .valueExpr = valueExpr;
053: this .formatters = formatters;
054: }
055:
056: public void addValue(Object s, long count) {
057: min = comparator.compare(s, min) < 0 ? s : min;
058: max = comparator.compare(s, max) > 0 ? s : max;
059: queryValue = null;
060: userValue = null;
061: this .count += count;
062: }
063:
064: public void merge(CountGroup g1) {
065: min = comparator.compare(min, g1.min) < 0 ? min : g1.min;
066: max = comparator.compare(max, g1.max) > 0 ? max : g1.max;
067: queryValue = null;
068: userValue = null;
069: count += g1.count;
070: }
071:
072: public static void merge(SortedMap<Object, CountGroup> map) {
073: if (map.size() > 1) {
074: CountGroup destination = map.get(map.lastKey());
075: SortedMap<Object, CountGroup> toBeMerged = map.headMap(map
076: .lastKey());
077: for (Object key : toBeMerged.keySet())
078: destination.merge(toBeMerged.get(key));
079:
080: toBeMerged.clear();
081: }
082: }
083:
084: public long getCount() {
085: return count;
086: }
087:
088: public void setCount(long count) {
089: this .count = count;
090: }
091:
092: public Object getMax() {
093: return max;
094: }
095:
096: public void setMax(Object max) {
097: this .max = max;
098: }
099:
100: public Object getMin() {
101: return min;
102: }
103:
104: public void setMin(Object min) {
105: this .min = min;
106: }
107:
108: public String getQueryValue() {
109: if (queryValue == null) {
110: queryValue = facetSampler.createQueryFormat(this ,
111: valueExpr, formatters.get("query"));
112: }
113: return queryValue;
114: }
115:
116: public String getUserValue() {
117: if (userValue == null) {
118: userValue = facetSampler.createUserFormat(this , valueExpr,
119: formatters.get("value"));
120: }
121: return userValue;
122: }
123: }
|