View Javadoc

1   package org.apache.helix.healthcheck;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.StringTokenizer;
23  
24  import org.apache.log4j.Logger;
25  
26  public class AggregationTypeFactory
27  {
28    private static final Logger logger = Logger
29        .getLogger(AggregationTypeFactory.class);
30  
31    public AggregationTypeFactory()
32    {
33    }
34  
35    // TODO: modify this function so that it takes a single string, but can parse
36    // apart params from type
37    public static AggregationType getAggregationType(String input)
38    {
39      if (input == null)
40      {
41        logger.error("AggregationType name is null");
42        return null;
43      }
44      StringTokenizer tok = new StringTokenizer(input, AggregationType.DELIM);
45      String type = tok.nextToken();
46      int numParams = tok.countTokens();
47      String[] params = new String[numParams];
48      for (int i = 0; i < numParams; i++)
49      {
50        if (!tok.hasMoreTokens())
51        {
52          logger.error("Trying to parse non-existent params");
53          return null;
54        }
55        params[i] = tok.nextToken();
56      }
57  
58      if (type.equals(AccumulateAggregationType.TYPE_NAME))
59      {
60        return new AccumulateAggregationType();
61      }
62      else if (type.equals(DecayAggregationType.TYPE_NAME))
63      {
64        if (params.length < 1)
65        {
66          logger
67              .error("DecayAggregationType must contain <decay weight> parameter");
68          return null;
69        }
70        return new DecayAggregationType(Double.parseDouble(params[0]));
71      }
72      else if (type.equals(WindowAggregationType.TYPE_NAME))
73      {
74        if (params.length < 1)
75        {
76          logger
77              .error("WindowAggregationType must contain <window size> parameter");
78        }
79        return new WindowAggregationType(Integer.parseInt(params[0]));
80      }
81      else
82      {
83        logger.error("Unknown AggregationType " + type);
84        return null;
85      }
86    }
87  }