001/* 002 * (C) Copyright 2009-2010 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Radu Darlea 016 * Florent Guillaume 017 */ 018 019package org.nuxeo.ecm.platform.tag; 020 021import org.apache.commons.lang.builder.EqualsBuilder; 022import org.apache.commons.lang.builder.HashCodeBuilder; 023 024import java.io.Serializable; 025import java.util.Comparator; 026 027/** 028 * Aggregates a tag with its weight. 029 */ 030public class Tag implements Serializable { 031 032 private static final long serialVersionUID = 1L; 033 034 /** 035 * The tag label. 036 */ 037 public final String label; 038 039 /** 040 * The weight of the tag. 041 */ 042 public long weight; 043 044 public Tag(String label, int weight) { 045 this.label = label; 046 this.weight = weight; 047 } 048 049 public String getLabel() { 050 return label; 051 } 052 053 public long getWeight() { 054 return weight; 055 } 056 057 public void setWeight(long weight) { 058 this.weight = weight; 059 } 060 061 @Override 062 public int hashCode() { 063 return HashCodeBuilder.reflectionHashCode(this); 064 } 065 066 @Override 067 public boolean equals(Object o) { 068 return EqualsBuilder.reflectionEquals(this, o); 069 } 070 071 @Override 072 public String toString() { 073 return getClass().getSimpleName() + '(' + label + ',' + weight + ')'; 074 } 075 076 protected static class TagLabelComparator implements Comparator<Tag> { 077 public int compare(Tag t1, Tag t2) { 078 return t1.label.compareToIgnoreCase(t2.label); 079 } 080 } 081 082 /** 083 * Compare tags by label, case insensitive. 084 */ 085 public static final Comparator<Tag> LABEL_COMPARATOR = new TagLabelComparator(); 086 087 protected static class TagWeightComparator implements Comparator<Tag> { 088 public int compare(Tag t1, Tag t2) { 089 return t2.weight < t1.weight ? -1 : (t2.weight == t1.weight ? 0 : 1); 090 } 091 } 092 093 /** 094 * Compare tags by weight, decreasing. 095 */ 096 public static final Comparator<Tag> WEIGHT_COMPARATOR = new TagWeightComparator(); 097 098}