All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.metrics.Field Maven / Gradle / Ivy

There is a newer version: 3.10.0-rc4
Show newest version
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.metrics;

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.base.Function;
import com.google.common.base.Functions;

/**
 * Describes a bucketing field used by a metric.
 *
 * @param  type of field
 */
public class Field {
  /**
   * Break down metrics by boolean true/false.
   *
   * @param name field name
   * @return boolean field
   */
  public static Field ofBoolean(String name) {
    return ofBoolean(name, null);
  }

  /**
   * Break down metrics by boolean true/false.
   *
   * @param name field name
   * @param description field description
   * @return boolean field
   */
  public static Field ofBoolean(String name, String description) {
    return new Field<>(name, Boolean.class, description);
  }

  /**
   * Break down metrics by cases of an enum.
   *
   * @param enumType type of enum
   * @param name field name
   * @return enum field
   */
  public static > Field ofEnum(Class enumType, String name) {
    return ofEnum(enumType, name, null);
  }

  /**
   * Break down metrics by cases of an enum.
   *
   * @param enumType type of enum
   * @param name field name
   * @param description field description
   * @return enum field
   */
  public static > Field ofEnum(
      Class enumType, String name, String description) {
    return new Field<>(name, enumType, description);
  }

  /**
   * Break down metrics by string.
   *
   * 

Each unique string will allocate a new submetric. Do not use user content as a field * value as field values are never reclaimed. * * @param name field name * @return string field */ public static Field ofString(String name) { return ofString(name, null); } /** * Break down metrics by string. * *

Each unique string will allocate a new submetric. Do not use user content as a field * value as field values are never reclaimed. * * @param name field name * @param description field description * @return string field */ public static Field ofString(String name, String description) { return new Field<>(name, String.class, description); } /** * Break down metrics by integer. * *

Each unique integer will allocate a new submetric. Do not use user content as a field * value as field values are never reclaimed. * * @param name field name * @return integer field */ public static Field ofInteger(String name) { return ofInteger(name, null); } /** * Break down metrics by integer. * *

Each unique integer will allocate a new submetric. Do not use user content as a field * value as field values are never reclaimed. * * @param name field name * @param description field description * @return integer field */ public static Field ofInteger(String name, String description) { return new Field<>(name, Integer.class, description); } private final String name; private final Class keyType; private final Function formatter; private final String description; private Field(String name, Class keyType, String description) { checkArgument(name.matches("^[a-z_]+$"), "name must match [a-z_]"); this.name = name; this.keyType = keyType; this.formatter = initFormatter(keyType); this.description = description; } /** @return name of this field within the metric. */ public String getName() { return name; } /** @return type of value used within the field. */ public Class getType() { return keyType; } /** @return description text for the field explaining its range of values. */ public String getDescription() { return description; } public Function formatter() { return formatter; } @SuppressWarnings("unchecked") private static Function initFormatter(Class keyType) { if (keyType == String.class) { return (Function) Functions.identity(); } else if (keyType == Integer.class || keyType == Boolean.class) { return (Function) Functions.toStringFunction(); } else if (Enum.class.isAssignableFrom(keyType)) { return in -> ((Enum) in).name(); } throw new IllegalStateException("unsupported type " + keyType.getName()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy