Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / LabeledValueImpl.java @ 2376

History | View | Annotate | Download (1.32 KB)

1
package org.gvsig.tools.util;
2

    
3
import org.apache.commons.lang3.StringUtils;
4

    
5
public class LabeledValueImpl<T> implements LabeledValue<T> , Comparable {
6

    
7
    protected String label;
8
    protected T value;
9
    protected int maxlen;
10

    
11
    public LabeledValueImpl(String label, T value, int maxlen) {
12
        this.label = label;
13
        this.value = value;
14
        this.maxlen = maxlen;
15
    }
16

    
17
    public LabeledValueImpl(String label, T value) {
18
        this(label,value,-1);
19
    }
20
    
21
    @Override
22
    public String toString() {
23
        return this.getLabel();
24
    }
25

    
26
    @Override
27
    public T getValue() {
28
        return this.value;
29
    }
30
    
31
    @Override
32
    public String getLabel() {
33
        if( this.maxlen<0 ) {
34
            return this.label;
35
        }
36
        return StringUtils.abbreviate(this.label,this.maxlen);
37
    }
38

    
39
    @Override
40
    @SuppressWarnings("StringEquality")
41
    public int compareTo(Object other) {
42
        String otherLabel = null;
43
        if( other!=null ) {
44
            otherLabel = other.toString();
45
        }
46
        String thisLabel = this.getLabel();
47
        if (thisLabel== otherLabel) {
48
            return 0;
49
        } else if (thisLabel== null) {
50
            return -1;
51
        } else if (otherLabel == null) {
52
            return 1;
53
        }
54
        return thisLabel.compareTo(otherLabel);        
55
    }
56
    
57
}