/** * The keys of the map will be List instead of arrays, since arrays don't give a decent hash * code. */ @Override public Map toMap() { Map<List<Object>, Object> result = new HashMap<>(); for (Map.Entry<List<Object>, CalcResult> item : results.entrySet()) { result.put(item.getKey(), item.getValue().getValue()); } return result; }
private Object[] entryToArray(Map.Entry<List<Object>, CalcResult> entry) { Object[] result = Arrays.copyOf(entry.getKey().toArray(), entry.getKey().size() + 1); result[entry.getKey().size()] = entry.getValue().getValue(); return result; } }
public CalcResult merge(CalcResult resultsToAdd) { if (!isCompatible(resultsToAdd)) { throw new IllegalArgumentException("Parameter is not a compatible type"); } if (resultsToAdd == CalcResult.NULL_RESULT) { return this; } if (resultsToAdd instanceof RangeResult) { // add one set to the other (to create one big unique list) Set newSet = new HashSet(ranges); newSet.addAll((Set) resultsToAdd.getValue()); return new RangeResult(newSet); } else { throw new IllegalArgumentException( "The CalcResults claim to be compatible, but the appropriate merge method has not been implemented."); } } }
@SuppressWarnings("unchecked") public Object evaluate(Object feature) { if (feature == null) { return Integer.valueOf(0); // no features were visited in the making of this answer } FeatureCollection<FeatureType, Feature> featureCollection = (FeatureCollection<FeatureType, Feature>) feature; synchronized (featureCollection) { if (featureCollection != previousFeatureCollection) { previousFeatureCollection = featureCollection; average = null; try { CalcResult result = calculateAverage(featureCollection, expr); if (result != null) { average = result.getValue(); } } catch (IllegalFilterException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } catch (IOException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } } } return average; }
public Object evaluate(Object feature) { if (feature == null) { return Integer.valueOf(0); // no features were visited in the making of this answer } SimpleFeatureCollection featureCollection = (SimpleFeatureCollection) feature; synchronized (featureCollection) { if (featureCollection != previousFeatureCollection) { previousFeatureCollection = featureCollection; count = null; try { CalcResult result = calculateCount(featureCollection); if (result != null) { count = result.getValue(); } } catch (IllegalFilterException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } catch (IOException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } } } return count; } }
@SuppressWarnings("unchecked") public Object evaluate(Object feature) { if (feature == null) { return Integer.valueOf(0); // no features were visited in the making of this answer } FeatureCollection<FeatureType, Feature> featureCollection = (FeatureCollection<FeatureType, Feature>) feature; synchronized (featureCollection) { if (featureCollection != previousFeatureCollection) { previousFeatureCollection = featureCollection; bounds = null; try { CalcResult result = calculateBounds(featureCollection); if (result != null) { bounds = result.getValue(); } } catch (IllegalFilterException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } catch (IOException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } } } return bounds; } }
public CalcResult merge(CalcResult resultsToAdd) { if (!isCompatible(resultsToAdd)) { throw new IllegalArgumentException("Parameter is not a compatible type"); } if (resultsToAdd == CalcResult.NULL_RESULT) { return this; } if (resultsToAdd instanceof UniqueResult) { // add one set to the other (to create one big unique list) Set newSet = createNewSet(unique, preserveOrder); newSet.addAll((Set) resultsToAdd.getValue()); return new UniqueResult(newSet, preserveOrder); } else { throw new IllegalArgumentException( "The CalcResults claim to be compatible, but the appropriate merge method has not been implemented."); } } }
public Object evaluate(Object feature) { if (feature == null) { return Integer.valueOf(0); // no features were visited in the making of this answer } SimpleFeatureCollection featureCollection = (SimpleFeatureCollection) feature; Expression expr = (Expression) getExpression(0); synchronized (featureCollection) { if (featureCollection != previousFeatureCollection) { previousFeatureCollection = featureCollection; unique = null; try { CalcResult result = calculateUnique(featureCollection, expr); if (result != null) { unique = result.getValue(); } } catch (IllegalFilterException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } catch (IOException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } } } return unique; }
public Object evaluate(Object feature) { if (feature == null) { return Integer.valueOf(0); // no features were visited in the making of this answer } Expression expr = (Expression) getExpression(0); SimpleFeatureCollection featureCollection = (SimpleFeatureCollection) feature; synchronized (featureCollection) { if (featureCollection != previousFeatureCollection) { previousFeatureCollection = featureCollection; max = null; try { CalcResult result = calculateMax(featureCollection, expr); if (result != null) { max = result.getValue(); } } catch (IllegalFilterException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } catch (IOException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } } } return max; }
public Object evaluate(Object feature) { if (feature == null) { return Integer.valueOf(0); // no features were visited in the making of this answer } Expression expr = (Expression) getExpression(0); SimpleFeatureCollection featureCollection = (SimpleFeatureCollection) feature; synchronized (featureCollection) { if (featureCollection != previousFeatureCollection) { previousFeatureCollection = featureCollection; min = null; try { CalcResult result = calculateMin(featureCollection, expr); if (result != null) { min = result.getValue(); } } catch (IllegalFilterException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } catch (IOException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } } } return min; }
public Object evaluate(Object feature) { if (feature == null) { return Integer.valueOf(0); // no features were visited in the making of this answer } SimpleFeatureCollection featureCollection = (SimpleFeatureCollection) feature; Expression expr = (Expression) getExpression(0); synchronized (featureCollection) { if (featureCollection != previousFeatureCollection) { previousFeatureCollection = featureCollection; sum = null; try { CalcResult result = calculateSum(featureCollection, expr); if (result != null) { sum = result.getValue(); } } catch (IllegalFilterException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } catch (IOException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } } } return sum; }
public Object evaluate(Object feature) { if (feature == null) { return Integer.valueOf(0); // no features were visited in the making of this answer } Expression expr = (Expression) getExpression(0); FeatureCollection<? extends FeatureType, ? extends Feature> featureCollection = (SimpleFeatureCollection) feature; synchronized (featureCollection) { if (featureCollection != previousFeatureCollection) { previousFeatureCollection = featureCollection; median = null; try { CalcResult result = calculateMedian(featureCollection, expr); if (result != null) { median = result.getValue(); } } catch (IllegalFilterException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } catch (IOException e) { LOGGER.log(Level.FINER, e.getLocalizedMessage(), e); } } } return median; }
private Object findDefaultValue(Query query, String attribute) { FeatureCalc aggFunc; switch (NetCDFUtilities.getParameterBehaviour(attribute)) { case MAX: aggFunc = new MaxVisitor(attribute); break; case MIN: aggFunc = new MinVisitor(attribute); break; default: return null; } try { request.source.reader.getCatalog().computeAggregateFunction(query, aggFunc); return aggFunc.getResult().getValue(); } catch (IOException e) { LOGGER.log(Level.WARNING, e.getMessage(), e); return null; } }
private Object calculate(SimpleFeatureCollection featureCollection) { FeatureCalc quantityVisitor = getListVisitor(); if (progress == null) progress = new NullProgressListener(); try { featureCollection.accepts(quantityVisitor, progress); } catch (IOException e) { LOGGER.log( Level.SEVERE, "QuantileFunction calculate(SimpleFeatureCollection) failed", e); return null; } if (progress.isCanceled()) return null; CalcResult calcResult = quantityVisitor.getResult(); if (calcResult == null || calcResult == CalcResult.NULL_RESULT) { return null; } List[] bin = (List[]) calcResult.getValue(); // generate the min and max values, and round off if applicable/necessary Comparable globalMin = (Comparable) bin[0].toArray()[0]; Object lastBin[] = bin[bin.length - 1].toArray(); if (lastBin.length == 0) { return null; } Comparable globalMax = (Comparable) lastBin[lastBin.length - 1]; if ((globalMin instanceof Number) && (globalMax instanceof Number)) { return calculateNumerical(bin, globalMin, globalMax); } else { return calculateNonNumerical(bin, globalMin, globalMax); } }
@Test public void onlyNotNulls() throws Exception { int idx = 1; for (T value : values) { featureCollection.add( featureBuilder.buildFeature("f" + idx, new Object[] {value, idx})); ++idx; } FeatureCalc calc = createVisitor(0, featureType); featureCollection.accepts(calc, null); Object value = calc.getResult().getValue(); assertEquals(expectedValue, value); assertSame(expectedValue.getClass(), value.getClass()); }
@Test public void emptyCollection() throws Exception { FeatureCalc calc = createVisitor(0, featureType); featureCollection.accepts(calc, null); assertNull(calc.getResult().getValue()); }
@Test public void testMoreClassesThanFeatures() throws SchemaException, IOException { ListFeatureCollection fc = getSimplifiedStatesCollection(); Expression areaFunction = EqualAreaFunction.getCartesianAreaFunction(); // twice as many classes EqualAreaListVisitor visitor = new EqualAreaListVisitor(PERSONS, areaFunction, fc.size() * 2); fc.accepts(visitor, null); List<Double>[] result = (List<Double>[]) visitor.getResult().getValue(); // check the number of classes has been reduced (43 not 49, not always possible to // fill all bins when there are so many classes) assertEquals(result.length, 43); } }
@Test public void mixed() throws Exception { int idx = 1; for (T value : values) { featureCollection.add( featureBuilder.buildFeature("f" + idx, new Object[] {value, idx})); ++idx; featureCollection.add(featureBuilder.buildFeature("f" + idx, new Object[] {null, idx})); ++idx; } FeatureCalc calc = createVisitor(0, featureType); featureCollection.accepts(calc, null); Object value = calc.getResult().getValue(); assertEquals(expectedValue, value); assertSame(expectedValue.getClass(), value.getClass()); } }
@Test public void onlyNulls() throws Exception { featureCollection.add(featureBuilder.buildFeature("f1", new Object[] {null, 1})); featureCollection.add(featureBuilder.buildFeature("f2", new Object[] {null, 2})); featureCollection.add(featureBuilder.buildFeature("f3", new Object[] {null, 3})); FeatureCalc calc = createVisitor(0, featureType); featureCollection.accepts(calc, null); assertNull(calc.getResult().getValue()); }
public void testQuantileList() throws Exception { FilterFactory factory = CommonFactoryFinder.getFilterFactory(null); Expression expr = factory.property(ft.getDescriptor(0).getLocalName()); QuantileListVisitor visitor = new QuantileListVisitor(expr, 2); fc.accepts(visitor, null); CalcResult result = visitor.getResult(); List[] qResult = (List[]) result.getValue(); assertEquals(2, qResult.length); assertEquals(2, qResult[0].size()); assertEquals(1, qResult[1].size()); // test empty collection QuantileListVisitor emptyVisitor = new QuantileListVisitor(expr, 2); empty.accepts(emptyVisitor, null); assertEquals(CalcResult.NULL_RESULT, emptyVisitor.getResult()); // test merge assertSame(result, emptyVisitor.getResult().merge(result)); assertSame(result, result.merge(emptyVisitor.getResult())); }