1 /*
   2  * Copyright (c) 2014, 2015, Dynatrace and/or its affiliates. All rights reserved.
   3  * 
   4  * This file is part of the Lock Contention Tracing Subsystem for the HotSpot
   5  * Virtual Machine, which is developed at Christian Doppler Laboratory on
   6  * Monitoring and Evolution of Very-Large-Scale Software Systems. Please
   7  * contact us at <http://mevss.jku.at/> if you need additional information
   8  * or have any questions.
   9  *
  10  * This code is free software; you can redistribute it and/or modify it
  11  * under the terms of the GNU General Public License version 2 only, as
  12  * published by the Free Software Foundation.
  13  *
  14  * This code is distributed in the hope that it will be useful, but WITHOUT
  15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17  * version 2 for more details (a copy is included in the LICENSE file that
  18  * accompanied this code).
  19  *
  20  * You should have received a copy of the GNU General Public License version
  21  * 2 along with this work. If not, see <http://www.gnu.org/licenses/>.
  22  *
  23  */ 
  24 package sun.evtracing.processing.statistics.aggregator;
  25 
  26 public abstract class AggregatorFactory<T extends Aggregator> {
  27         private final AggregatorFactory<? extends Aggregator> nextFactory;
  28         private final AggregationConfiguration configuration;
  29 
  30         public AggregatorFactory(AggregationConfiguration configuration) {
  31                 this(null, configuration);
  32         }
  33 
  34         public AggregatorFactory(AggregatorFactory<? extends Aggregator> nextFactory, AggregationConfiguration configuration) {
  35                 this.nextFactory = nextFactory;
  36                 this.configuration = configuration;
  37         }
  38 
  39         public abstract T create();
  40 
  41         public abstract T merge(T x, T y);
  42 
  43         public AggregationConfiguration configuration() {
  44                 return configuration;
  45         }
  46 
  47         T mergeChecked(Aggregator x, Aggregator y) {
  48                 if (x.factory() != this) {
  49                         throw new IllegalArgumentException("aggregator x not an instance of factory");
  50                 }
  51                 if (y.factory() != this) {
  52                         throw new IllegalArgumentException("aggregator y not an instance of factory");
  53                 }
  54                 @SuppressWarnings("unchecked")
  55                 T xt = (T) x;
  56                 @SuppressWarnings("unchecked")
  57                 T yt = (T) y;
  58                 return merge(xt, yt);
  59         }
  60 
  61         AggregatorFactory<? extends Aggregator> nextFactory() {
  62                 return nextFactory;
  63         }
  64 }