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

com.arpnetworking.metrics.impl.TsdUnit Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
/**
 * Copyright 2015 Groupon.com
 *
 * 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.arpnetworking.metrics.impl;

import com.arpnetworking.metrics.Unit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import javax.annotation.Nullable;

/**
 * Default implementation of Unit.
 *
 * @author Ville Koskela (vkoskela at groupon dot com)
 */
public final class TsdUnit implements Unit {

    /**
     * {@inheritDoc}
     */
    @Override
    public String getName() {
        return _baseScale.getName() + _baseUnit.getName();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(final Object other) {
        if (this == other) {
            return true;
        }

        if (!(other instanceof TsdUnit)) {
            return false;
        }

        final TsdUnit otherUnit = (TsdUnit) other;
        return Objects.equals(_baseUnit, otherUnit._baseUnit)
                && Objects.equals(_baseScale, otherUnit._baseScale);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return Objects.hash(_baseUnit, _baseScale);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return String.format(
                "TsdUnit{BaseUnit=%s, BaseScale=%s}",
                _baseUnit,
                _baseScale);
    }

    private TsdUnit(final Builder builder) {
        _baseUnit = builder._baseUnit;
        _baseScale = builder._baseScale;
    }

    private final BaseUnit _baseUnit;
    private final BaseScale _baseScale;

    private static final Logger LOGGER = LoggerFactory.getLogger(TsdUnit.class);

    /**
     * Builder for TsdScaledUnit.
     *
     * @author Ville Koskela (vkoskela at groupon dot com)
     */
    public static final class Builder {

        /**
         * Create an instance of Unit. The instance may be
         * null if no base unit is specified.
         *
         * Optional is empty if the Unit reduces to
         * a constant (e.g. Byte / Byte).
         *
         * Create an instance of Unit.
         *
         * @return Instance of Unit.
         */
        @Nullable public Unit build() {
            if (_baseScale == null && _baseUnit == null) {
                return null;
            }
            if (_baseScale == null) {
                return _baseUnit;
            }
            if (_baseUnit == null) {
                // TODO(vkoskela): Support scalar only units.
                LOGGER.warn("TsdUnit cannot be constructed without base unit");
                return null;
            }
            return new TsdUnit(this);
        }

        /**
         * Set the base unit.
         *
         * @param value The base unit.
         * @return This Builder instance.
         */
        public Builder setBaseUnit(final BaseUnit value) {
            _baseUnit = value;
            return this;
        }

        /**
         * Set the base scale.
         *
         * @param value The base scale.
         * @return This Builder instance.
         */
        public Builder setScale(final BaseScale value) {
            _baseScale = value;
            return this;
        }

        private BaseUnit _baseUnit;
        private BaseScale _baseScale;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy