Java tutorial
/* * Copyright (c) 2004-2009 SMG Co., Ltd. All Rights Reserved. * Please read the associated COPYRIGHTS file for more details. * * THE SOFTWARE IS PROVIDED BY SMG Co., Ltd., WITHOUT WARRANTY OF * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY * CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ package jp.co.acroquest.endosnipe.report.converter.compressor; import java.lang.reflect.InvocationTargetException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Date; import java.util.List; import jp.co.acroquest.endosnipe.report.converter.util.calc.Calculator; import jp.co.acroquest.endosnipe.report.converter.util.calc.CalculatorFactory; import jp.co.acroquest.endosnipe.report.util.ReporterConfigAccessor; import jp.co.acroquest.endosnipe.report.converter.compressor.CompressOperation; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.PropertyUtils; /** * ???Excel?????? * * @author M.Yoshida */ @SuppressWarnings("unchecked") public class SamplingCompressor { private static final String MAX_VALUE_PROPERTY_SUFFIX_ = "Max"; private static final String MIN_VALUE_PROPERTY_SUFFIX_ = "Min"; private static final String SAMPLING_MAX_NUM_KEY_ = "reporter.report.maxSamples"; /** ???? */ private long minLimitSamplingTerm_ = 5000; /** ?[sec] */ private long rawSamplingTerm_ = 5; /** ?? */ private long samplingMax_ = 0; private static long SEC_PER_MILLIS = 1000; /** ? */ private static long DEFAULT_SAMPLING_MAX = 200; /** * */ public SamplingCompressor() { this.samplingMax_ = Long.parseLong(ReporterConfigAccessor.getProperty(SAMPLING_MAX_NUM_KEY_)); if (this.samplingMax_ <= 0) { this.samplingMax_ = DEFAULT_SAMPLING_MAX; } } /** * * * @param minTerm ???? */ public SamplingCompressor(long minTerm) { super(); this.minLimitSamplingTerm_ = minTerm; } /** * ?????? * ??????????????? * * @param samplingList measureTimeField?????????????? * @param startTime ? * @param endTime ? * @param measureTimeField ???? * @param operation ?? * @param clazz ?Class * @return ??? * @throws IllegalAccessException ????????? * @throws InvocationTargetException ?????? * @throws NoSuchMethodException ?????? * @throws NoSuchFieldException * @throws InstantiationException * @throws SecurityException */ public List compressSamplingList(List samplingList, Timestamp startTime, Timestamp endTime, String measureTimeField, List<CompressOperation> operation, Class clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, SecurityException, InstantiationException, NoSuchFieldException { // ?1???? // ??????????????????? long samplingTerm = (endTime.getTime() - startTime.getTime()) / this.samplingMax_; if (samplingTerm < this.minLimitSamplingTerm_) { samplingTerm = this.minLimitSamplingTerm_; } long nowStartTime = startTime.getTime(); int sampleIndex = 0; List compressedList = new ArrayList(); while (nowStartTime < endTime.getTime()) { // ????? List samplingGroup = new ArrayList(); for (int cnt = sampleIndex; cnt < samplingList.size(); cnt++) { Object sampleData = samplingList.get(cnt); Date measureTime = (Date) PropertyUtils.getProperty(sampleData, measureTimeField); if (nowStartTime > measureTime.getTime()) { sampleIndex++; continue; } if (nowStartTime + samplingTerm <= measureTime.getTime()) { sampleIndex = cnt; break; } samplingGroup.add(sampleData); } // ?? Object compressedData = createCompressedSample(samplingGroup, nowStartTime, nowStartTime + samplingTerm, measureTimeField, operation, clazz); compressedList.add(compressedData); nowStartTime += samplingTerm; } return compressedList; } /** * ?????????? * ??????????????? * * @param rawSamples * @param targetFields ? * @return ?? * @throws IllegalAccessException ????????? * @throws InvocationTargetException ?????? * @throws NoSuchMethodException ?????? * @throws InstantiationException * @throws NoSuchFieldException * @throws SecurityException */ private Object createCompressedSample(List rawSamples, long startTime, long endTime, String measureTimeField, List<CompressOperation> operation, Class clazz) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException, SecurityException, NoSuchFieldException { Object returnSample; if (rawSamples.size() < 1) { returnSample = clazz.newInstance(); } else { returnSample = rawSamples.get(0); } BeanUtils.setProperty(returnSample, measureTimeField, new Timestamp(startTime)); for (CompressOperation ope : operation) { Object maxObj = getMaxValueFromSampleList(rawSamples, ope.getCompressField(), clazz); Object minObj = getMinValueFromSampleList(rawSamples, ope.getCompressField(), clazz); Object compressedObj = getCompressedValue(rawSamples, ope, clazz, endTime - startTime); PropertyUtils.setProperty(returnSample, ope.getCompressField() + MAX_VALUE_PROPERTY_SUFFIX_, maxObj); PropertyUtils.setProperty(returnSample, ope.getCompressField() + MIN_VALUE_PROPERTY_SUFFIX_, minObj); PropertyUtils.setProperty(returnSample, ope.getCompressField(), compressedObj); } return returnSample; } /** * ???????? * * @param rawSamples ? * @param targetField ? * @return ?? * @throws IllegalAccessException ????????? * @throws InvocationTargetException ?????? * @throws NoSuchMethodException ?????? * @throws NoSuchFieldException * @throws SecurityException * @throws InstantiationException */ private Object getMaxValueFromSampleList(List rawSamples, String targetField, Class clazz) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException, InstantiationException { if (rawSamples.size() < 1) { Calculator calculator = getFieldCalculator(clazz, targetField); return calculator.immediate("0"); } List values = getValuesByFieldName(rawSamples, targetField); if (!(values.get(0) instanceof Comparable)) { return values.get(0); } Comparable maxValue = (Comparable) values.get(0); for (Object value : values) { if (maxValue.compareTo(value) < 0) { maxValue = (Comparable) value; } } return maxValue; } /** * ????????? * * @param rawSamples ? * @param targetField ?? * @return ??? * @throws IllegalAccessException ????????? * @throws InvocationTargetException ?????? * @throws NoSuchMethodException ?????? * @throws NoSuchFieldException * @throws SecurityException * @throws InstantiationException */ private Object getMinValueFromSampleList(List rawSamples, String targetField, Class clazz) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException, InstantiationException { if (rawSamples.size() < 1) { Calculator calculator = getFieldCalculator(clazz, targetField); return calculator.immediate("0"); } List values = getValuesByFieldName(rawSamples, targetField); if (!(values.get(0) instanceof Comparable)) { return values.get(0); } Comparable minValue = (Comparable) values.get(0); for (Object value : values) { if (minValue.compareTo(value) > 0) { minValue = (Comparable) value; } } return minValue; } /** * ????? * * @param rawSamples ?? * @param operation ?? * @param clazz ? * @param compressedTerm ?? * @return ?? * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws InstantiationException */ private Object getCompressedValue(List rawSamples, CompressOperation operation, Class clazz, long compressedTerm) throws SecurityException, NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException { Calculator calculator = getFieldCalculator(clazz, operation.getCompressField()); if (rawSamples.size() < 1) { return calculator.immediate("0"); } List values = getValuesByFieldName(rawSamples, operation.getCompressField()); switch (operation.getOperation()) { case SIMPLE_AVERAGE: Object totalValue = calcTotal(values, calculator); return calculator.div(totalValue, calculator.immediate(String.valueOf(values.size()))); case TOTAL: return calcTotal(values, calculator); case TIME_AVERAGE: Object multipler = calculator.immediate(String.valueOf(this.rawSamplingTerm_)); Object productSumValue = calcProductSum(values, multipler, calculator); Object samplingTerm = calculator.immediate(String.valueOf(compressedTerm)); samplingTerm = calculator.div(samplingTerm, calculator.immediate(String.valueOf(SEC_PER_MILLIS))); return calculator.div(productSumValue, samplingTerm); } return null; } /** * ??????? * * @param values ? * @param calculator * @return ? */ private Object calcTotal(List values, Calculator calculator) { if (values.size() < 1) { return calculator.immediate("0"); } if (values.size() == 1) { return values.get(0); } Object result = values.get(0); for (int cnt = 1; cnt < values.size(); cnt++) { result = calculator.add(result, values.get(cnt)); } return result; } /** * ?????????????? * * @param values ? * @param multipler ???????? * @param calculator * @return ?? */ private Object calcProductSum(List values, Object multipler, Calculator calculator) { if (values.size() < 1) { return calculator.immediate("0"); } if (values.size() == 1) { return calculator.mul(values.get(0), multipler); } Object result = calculator.mul(values.get(0), multipler); for (int cnt = 1; cnt < values.size(); cnt++) { Object adder = calculator.mul(values.get(cnt), multipler); result = calculator.add(result, adder); } return result; } /** * ?JavaBean???????????????? * ?? * * @param rawSamples JavaBean??? * @param targetField ?????? * @return ????? * @throws IllegalAccessException ????????? * @throws InvocationTargetException ?????? * @throws NoSuchMethodException ?????? */ private List getValuesByFieldName(List rawSamples, String targetField) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { List values = new ArrayList(); for (Object sample : rawSamples) { values.add(PropertyUtils.getProperty(sample, targetField)); } return values; } /** * ???????? * * @param clazz ?? * @param fieldName ??? * @return ????? * @throws NoSuchFieldException * @throws SecurityException * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws Exception ????????? */ private Calculator getFieldCalculator(Class clazz, String fieldName) throws SecurityException, NoSuchFieldException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object dummy = clazz.newInstance(); Object dummyObj = PropertyUtils.getProperty(dummy, fieldName); return CalculatorFactory.createCalculator(dummyObj.getClass()); } }