001/*
002 * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Radu Darlea
018 *     Florent Guillaume
019 */
020
021package org.nuxeo.ecm.platform.tag;
022
023import org.apache.commons.lang.builder.EqualsBuilder;
024import org.apache.commons.lang.builder.HashCodeBuilder;
025
026import java.io.Serializable;
027import java.util.Comparator;
028
029/**
030 * Aggregates a tag with its weight.
031 */
032public class Tag implements Serializable {
033
034    private static final long serialVersionUID = 1L;
035
036    /**
037     * The tag label.
038     */
039    public final String label;
040
041    /**
042     * The weight of the tag.
043     */
044    public long weight;
045
046    public Tag(String label, int weight) {
047        this.label = label;
048        this.weight = weight;
049    }
050
051    public String getLabel() {
052        return label;
053    }
054
055    public long getWeight() {
056        return weight;
057    }
058
059    public void setWeight(long weight) {
060        this.weight = weight;
061    }
062
063    @Override
064    public int hashCode() {
065        return HashCodeBuilder.reflectionHashCode(this);
066    }
067
068    @Override
069    public boolean equals(Object o) {
070        return EqualsBuilder.reflectionEquals(this, o);
071    }
072
073    @Override
074    public String toString() {
075        return getClass().getSimpleName() + '(' + label + ',' + weight + ')';
076    }
077
078    protected static class TagLabelComparator implements Comparator<Tag> {
079        public int compare(Tag t1, Tag t2) {
080            return t1.label.compareToIgnoreCase(t2.label);
081        }
082    }
083
084    /**
085     * Compare tags by label, case insensitive.
086     */
087    public static final Comparator<Tag> LABEL_COMPARATOR = new TagLabelComparator();
088
089    protected static class TagWeightComparator implements Comparator<Tag> {
090        public int compare(Tag t1, Tag t2) {
091            return t2.weight < t1.weight ? -1 : (t2.weight == t1.weight ? 0 : 1);
092        }
093    }
094
095    /**
096     * Compare tags by weight, decreasing.
097     */
098    public static final Comparator<Tag> WEIGHT_COMPARATOR = new TagWeightComparator();
099
100}