| author | Steffen Pingel | 2012-02-11 08:41:58 (EST) |
|---|---|---|
| committer | Steffen Pingel | 2012-02-11 08:41:58 (EST) |
| commit | fa6a5fb9967a08f54e5800a1b239c03bfb4850ba (patch) (side-by-side diff) | |
| tree | dcbc5b4518cc80209932a12944612dd29519d863 | |
| parent | a7f0e7fc780376a7ec5b21dd4a8a15d2ca015090 (diff) | |
| download | org.eclipse.mylyn.tasks-fa6a5fb9967a08f54e5800a1b239c03bfb4850ba.zip org.eclipse.mylyn.tasks-fa6a5fb9967a08f54e5800a1b239c03bfb4850ba.tar.gz org.eclipse.mylyn.tasks-fa6a5fb9967a08f54e5800a1b239c03bfb4850ba.tar.bz2 | |
NEW - bug 371010: [api] provide abstracted priority mapping
https://bugs.eclipse.org/bugs/show_bug.cgi?id=371010
Change-Id: I858678eac8c4803811553e18e65775244f61c78c
3 files changed, 149 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/ITask.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/ITask.java index f3d77ea..6cf0129 100644 --- a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/ITask.java +++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/ITask.java @@ -13,6 +13,7 @@ package org.eclipse.mylyn.tasks.core; import java.util.Date; +import org.eclipse.core.runtime.Assert; import org.eclipse.mylyn.internal.tasks.core.Messages; /** @@ -72,11 +73,29 @@ public interface ITask extends IRepositoryElement, IAttributeContainer { } /** + * Defines an interface for priorities that have an associated integer value. + * + * @author Steffen Pingel + * @since 3.7 + * @see PriorityLevel#fromValue(IPriorityValue[], IPriorityValue) + */ + public static interface IPriorityValue { + + /** + * Returns the integer value of this priority. + */ + int getPriorityValue(); + + } + + /** * @since 3.0 */ public enum PriorityLevel { P1, P2, P3, P4, P5; + private static final int LEVEL_COUNT = PriorityLevel.values().length; + @Override public String toString() { switch (this) { @@ -185,6 +204,34 @@ public interface ITask extends IRepositoryElement, IAttributeContainer { } /** + * Maps a priority value to a {@link PriorityLevel}. The value needs to be present in <code>priorities</code>, + * otherwise {@link PriorityLevel#getDefault()} is returned. + * <p> + * NOTE: <code>priorities</code> needs to be sorted in ascending order. + * + * @param priorities + * a sorted array of priority levels + * @param value + * the value to map + * @since 3.7 + */ + public static PriorityLevel fromValue(IPriorityValue[] priorities, IPriorityValue value) { + Assert.isNotNull(priorities); + if (value != null) { + int minValue = priorities[0].getPriorityValue(); + int range = priorities[priorities.length - 1].getPriorityValue() - minValue; + for (IPriorityValue priority : priorities) { + if (value.equals(priority)) { + float relativeValue = (float) (priority.getPriorityValue() - minValue) / range; + int level = (int) (relativeValue * LEVEL_COUNT) + 1; + return PriorityLevel.fromLevel(level); + } + } + } + return PriorityLevel.getDefault(); + } + + /** * @since 3.0 */ public static PriorityLevel getDefault() { diff --git a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/AllTasksTests.java b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/AllTasksTests.java index 7f36e08..abc4bd7 100644 --- a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/AllTasksTests.java +++ b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/AllTasksTests.java @@ -16,6 +16,7 @@ import junit.framework.TestSuite; import org.eclipse.mylyn.tasks.tests.core.FileTaskAttachmentSourceTest; import org.eclipse.mylyn.tasks.tests.core.ITasksCoreConstantsTest; +import org.eclipse.mylyn.tasks.tests.core.PriorityLevelTest; import org.eclipse.mylyn.tasks.tests.core.TaskListUnmatchedContainerTest; import org.eclipse.mylyn.tasks.tests.core.TaskRepositoryLocationTest; import org.eclipse.mylyn.tasks.tests.data.TaskDataExternalizerTest; @@ -118,6 +119,7 @@ public class AllTasksTests { //suite.addTestSuite(BackgroundSaveTest.class); suite.addTestSuite(MultipleTaskHyperlinkDetectorTest.class); suite.addTestSuite(RegionComparatorTest.class); + suite.addTestSuite(PriorityLevelTest.class); return suite; } diff --git a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/core/PriorityLevelTest.java b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/core/PriorityLevelTest.java new file mode 100644 index 0000000..9b9213d --- a/dev/null +++ b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/core/PriorityLevelTest.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright (c) 2012 Tasktop Technologies and others. + * 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: + * Tasktop Technologies - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.tasks.tests.core; + +import junit.framework.TestCase; + +import org.eclipse.mylyn.tasks.core.ITask.IPriorityValue; +import org.eclipse.mylyn.tasks.core.ITask.PriorityLevel; + +/** + * @author Steffen Pingel + */ +public class PriorityLevelTest extends TestCase { + + private static class Priority implements IPriorityValue { + + private final int value; + + public Priority(int value) { + this.value = value; + } + + public int getPriorityValue() { + return value; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + value; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Priority other = (Priority) obj; + if (value != other.value) { + return false; + } + return true; + } + + } + + public void testFromValueThreePriorities() { + Priority p1 = new Priority(1); + Priority p2 = new Priority(2); + Priority p3 = new Priority(3); + Priority[] priorities = new Priority[] { p1, p2, p3 }; + assertEquals("P1", PriorityLevel.fromValue(priorities, new Priority(1)).toString()); + assertEquals("P3", PriorityLevel.fromValue(priorities, new Priority(2)).toString()); + assertEquals("P5", PriorityLevel.fromValue(priorities, new Priority(3)).toString()); + assertEquals("P3", PriorityLevel.fromValue(priorities, new Priority(10)).toString()); + assertEquals("P3", PriorityLevel.fromValue(priorities, null).toString()); + } + + public void testFromValueSinglePriority() { + Priority p1 = new Priority(10); + Priority[] priorities = new Priority[] { p1 }; + assertEquals("P1", PriorityLevel.fromValue(priorities, new Priority(10)).toString()); + assertEquals("P3", PriorityLevel.fromValue(priorities, new Priority(11)).toString()); + assertEquals("P3", PriorityLevel.fromValue(priorities, null).toString()); + } + + public void testFromValueSixPriorites() { + Priority p1 = new Priority(10); + Priority p2 = new Priority(20); + Priority p3 = new Priority(30); + Priority p4 = new Priority(40); + Priority p5 = new Priority(70); + Priority p6 = new Priority(100); + Priority[] priorities = new Priority[] { p1, p2, p3, p4, p5, p6 }; + assertEquals("P1", PriorityLevel.fromValue(priorities, new Priority(10)).toString()); + assertEquals("P1", PriorityLevel.fromValue(priorities, new Priority(20)).toString()); + assertEquals("P2", PriorityLevel.fromValue(priorities, new Priority(30)).toString()); + assertEquals("P2", PriorityLevel.fromValue(priorities, new Priority(40)).toString()); + assertEquals("P4", PriorityLevel.fromValue(priorities, new Priority(70)).toString()); + assertEquals("P5", PriorityLevel.fromValue(priorities, new Priority(100)).toString()); + } + +} |

