Java tutorial
/* * Copyright (C) 2015 * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.cleverbus.spi.throttling; import org.cleverbus.api.common.HumanReadable; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; import org.springframework.util.Assert; /** * The class represents a throttling scope consisting of a source system and service name. * * @author <a href="mailto:petr.juza@cleverlance.com">Petr Juza</a> */ public final class ThrottleScope implements HumanReadable { /** * Represents any source system. */ public static final String ANY_SOURCE_SYSTEM = "any_system"; /** * Represents any service. */ public static final String ANY_SERVICE = "any_service"; private static final String ANY = "*"; public static final String THROTTLE_SEPARATOR = "."; private String sourceSystem; private String serviceName; /** * Creates new throttle scope. * * @param sourceSystem the source system, can be used {@link #ANY_SOURCE_SYSTEM} or '*' * @param serviceName the service name, can be used {@link #ANY_SERVICE} or '*' */ public ThrottleScope(String sourceSystem, String serviceName) { Assert.hasText(sourceSystem, "the sourceSystem must not be empty"); Assert.hasText(serviceName, "the serviceName must not be empty"); this.sourceSystem = sourceSystem.equals(ANY) ? ANY_SOURCE_SYSTEM : sourceSystem; this.serviceName = serviceName.equals(ANY) ? ANY_SERVICE : serviceName; } /** * Gets source system. * * @return source system */ public String getSourceSystem() { return sourceSystem; } /** * Gets service name. * * @return service name */ public String getServiceName() { return serviceName; } /** * Tests if the throttling scopes match. * * @return the match factor. Negative value signifies no match. * Non-negative signifies a match. The greater the returned value * the closer the match. */ public int match(final ThrottleScope that) { int factor = 0; if (StringUtils.equalsIgnoreCase(this.sourceSystem, that.sourceSystem)) { factor += 1; } else { if (this.sourceSystem != ANY_SOURCE_SYSTEM && that.sourceSystem != ANY_SOURCE_SYSTEM) { return -1; } } if (StringUtils.equalsIgnoreCase(this.serviceName, that.serviceName)) { factor += 1; } else { if (this.serviceName != ANY_SERVICE && that.serviceName != ANY_SERVICE) { return -1; } } return factor; } @Override public boolean equals(Object obj) { if (obj == this) { return true; } else if (obj instanceof ThrottleScope) { ThrottleScope en = (ThrottleScope) obj; return new EqualsBuilder().append(sourceSystem, en.sourceSystem).append(serviceName, en.serviceName) .isEquals(); } else { return false; } } @Override public int hashCode() { return new HashCodeBuilder(17, 37).append(sourceSystem).append(serviceName).toHashCode(); } @Override public String toHumanString() { return (sourceSystem.equals(ANY_SOURCE_SYSTEM) ? ANY : sourceSystem) + THROTTLE_SEPARATOR + (serviceName.equals(ANY_SERVICE) ? ANY : serviceName); } @Override public String toString() { return new ToStringBuilder(this).append("sourceSystem", sourceSystem).append("serviceName", serviceName) .toString(); } }