Skip to main content
summaryrefslogtreecommitdiffstats
blob: 03a2261370188a80a7b38b30c1c8dd9b109f19d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*******************************************************************************
 * Copyright (c) 2014 Obeo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.rcp.internal.extension.impl;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;

import org.eclipse.emf.compare.rcp.internal.extension.IItemDescriptor;

/**
 * Abstract class for an {@link IItemDescriptor}.
 * 
 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
 * @param <T>
 *            item type
 */
public abstract class AbstractItemDescriptor<T> implements IItemDescriptor<T> {

	/** EMPTY_STRING. */
	protected static final String EMPTY_STRING = ""; //$NON-NLS-1$

	/** Label of the item. */
	private final String label;

	/** Description of the item. */
	private final String description;

	/** Rank of the item. */
	private final int rank;

	/** ID of the item (Use as key for the registry). */
	private final String id;

	/**
	 * Constructor.
	 * 
	 * @param label
	 *            {@link AbstractItemDescriptor#label}
	 * @param description
	 *            {@link AbstractItemDescriptor#description}
	 * @param rank
	 *            {@link AbstractItemDescriptor#rank}
	 * @param id
	 *            {@link AbstractItemDescriptor#id}
	 */
	public AbstractItemDescriptor(String label, String description, int rank, String id) {
		super();
		Preconditions.checkNotNull(id);
		if (label != null) {
			this.label = label;
		} else {
			this.label = id;
		}
		if (description != null) {
			this.description = description;
		} else {
			this.description = EMPTY_STRING;
		}
		this.rank = rank;
		this.id = id;

	}

	/**
	 * {@inheritDoc}
	 */
	public String getLabel() {
		return label;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * {@inheritDoc}
	 */
	public int getRank() {
		return rank;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getID() {
		return id;
	}

	/**
	 * {@inheritDoc}
	 */
	public int compareTo(IItemDescriptor<T> o) {
		Preconditions.checkNotNull(o);
		int comp = getRank() - o.getRank();
		if (comp == 0) {
			comp = getID().compareTo(o.getID());
		}
		return comp;
	}

	/**
	 * get a {@link Function} to transform a descriptor into a item.
	 * 
	 * @param <T>
	 *            A item type
	 * @return A item
	 */
	public static <T> Function<IItemDescriptor<T>, T> getItemFunction() {
		return new Function<IItemDescriptor<T>, T>() {

			public T apply(IItemDescriptor<T> input) {
				if (input != null) {
					return input.getItem();
				}
				return null;
			}
		};
	}

}

Back to the top