diff options
author | Roberto E. Escobar | 2013-07-11 21:01:34 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2013-09-13 21:10:26 +0000 |
commit | 3a4a9175493addac6e4adfd8a1f111746b1ed647 (patch) | |
tree | cae1cd0eefe9039db0d7ca5adc0907f73dce47a1 /plugins | |
parent | 084007d42a021474f8f3ffbbddfc1a01038a7f79 (diff) | |
download | org.eclipse.osee-3a4a9175493addac6e4adfd8a1f111746b1ed647.tar.gz org.eclipse.osee-3a4a9175493addac6e4adfd8a1f111746b1ed647.tar.xz org.eclipse.osee-3a4a9175493addac6e4adfd8a1f111746b1ed647.zip |
feature[ats_7SNLZ]: Create common collection with filtering
Change-Id: Iec4948a73a3251a9808db39210053eaa289ade11
Diffstat (limited to 'plugins')
19 files changed, 625 insertions, 677 deletions
diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeCollectionTest.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeCollectionTest.java index 539e59803af..b7aa6518097 100644 --- a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeCollectionTest.java +++ b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeCollectionTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Boeing. + * Copyright (c) 2013 Boeing. * 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 @@ -28,8 +28,6 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import com.google.common.base.Optional; -import com.google.common.base.Predicate; import com.google.common.collect.Iterables; /** @@ -53,9 +51,9 @@ public class AttributeCollectionTest { MockitoAnnotations.initMocks(this); attributeCollection = new AttributeCollection(exceptionFactory); - attributeCollection.addAttribute(CoreAttributeTypes.Country, dirtyAttr); - attributeCollection.addAttribute(CoreAttributeTypes.Active, cleanAttr); - attributeCollection.addAttribute(CoreAttributeTypes.Annotation, deletedAttr); + attributeCollection.add(CoreAttributeTypes.Country, dirtyAttr); + attributeCollection.add(CoreAttributeTypes.Active, cleanAttr); + attributeCollection.add(CoreAttributeTypes.Annotation, deletedAttr); when(dirtyAttr.isDirty()).thenReturn(true); when(deletedAttr.isDeleted()).thenReturn(true); @@ -63,24 +61,24 @@ public class AttributeCollectionTest { @Test public void testGetAttributesDirty() throws OseeCoreException { - assertEquals(1, attributeCollection.getAttributeListDirties().size()); - assertEquals(dirtyAttr, attributeCollection.getAttributeListDirties().iterator().next()); + assertEquals(1, attributeCollection.getDirties().size()); + assertEquals(dirtyAttr, attributeCollection.getDirties().iterator().next()); } @Test public void testHasAttributesDirty() { - boolean actual1 = attributeCollection.hasAttributesDirty(); + boolean actual1 = attributeCollection.hasDirty(); assertTrue(actual1); - attributeCollection.removeAttribute(CoreAttributeTypes.Country, dirtyAttr); + attributeCollection.remove(CoreAttributeTypes.Country, dirtyAttr); - boolean actual2 = attributeCollection.hasAttributesDirty(); + boolean actual2 = attributeCollection.hasDirty(); assertFalse(actual2); } @Test public void testGetAllAttributes() { - List<Attribute<?>> attributes = attributeCollection.getAllAttributes(); + Collection<Attribute<?>> attributes = attributeCollection.getAll(); assertEquals(3, attributes.size()); assertTrue(attributes.contains(dirtyAttr)); @@ -103,7 +101,7 @@ public class AttributeCollectionTest { when(deletedAttr.getAttributeType()).thenReturn(typeC); when(deletedAttr.isDeleted()).thenReturn(false); - Collection<IAttributeType> types = attributeCollection.getExistingTypes(DeletionFlag.INCLUDE_DELETED); + Collection<? extends IAttributeType> types = attributeCollection.getExistingTypes(DeletionFlag.INCLUDE_DELETED); assertEquals(3, types.size()); @@ -111,7 +109,7 @@ public class AttributeCollectionTest { assertTrue(types.contains(typeB)); assertTrue(types.contains(typeC)); - Collection<IAttributeType> types2 = attributeCollection.getExistingTypes(DeletionFlag.EXCLUDE_DELETED); + Collection<? extends IAttributeType> types2 = attributeCollection.getExistingTypes(DeletionFlag.EXCLUDE_DELETED); assertEquals(1, types2.size()); assertFalse(types2.contains(typeA)); @@ -121,14 +119,14 @@ public class AttributeCollectionTest { @Test public void testGetListDeletionFlag() throws OseeCoreException { - List<Attribute<Object>> list1 = attributeCollection.getAttributeList(DeletionFlag.INCLUDE_DELETED); + List<Attribute<?>> list1 = attributeCollection.getList(DeletionFlag.INCLUDE_DELETED); assertEquals(3, list1.size()); assertTrue(list1.contains(dirtyAttr)); assertTrue(list1.contains(cleanAttr)); assertTrue(list1.contains(deletedAttr)); - List<Attribute<Object>> list2 = attributeCollection.getAttributeList(DeletionFlag.EXCLUDE_DELETED); + List<Attribute<?>> list2 = attributeCollection.getList(DeletionFlag.EXCLUDE_DELETED); assertEquals(2, list2.size()); assertTrue(list2.contains(dirtyAttr)); @@ -138,14 +136,14 @@ public class AttributeCollectionTest { @Test public void testGetSetDeletionFlag() throws OseeCoreException { - ResultSet<Attribute<Object>> set1 = attributeCollection.getAttributeSet(DeletionFlag.INCLUDE_DELETED); + ResultSet<Attribute<?>> set1 = attributeCollection.getResultSet(DeletionFlag.INCLUDE_DELETED); assertEquals(3, set1.size()); checkContains(set1, dirtyAttr, true); checkContains(set1, cleanAttr, true); checkContains(set1, deletedAttr, true); - ResultSet<Attribute<Object>> set2 = attributeCollection.getAttributeSet(DeletionFlag.EXCLUDE_DELETED); + ResultSet<Attribute<?>> set2 = attributeCollection.getResultSet(DeletionFlag.EXCLUDE_DELETED); assertEquals(2, set2.size()); checkContains(set2, dirtyAttr, true); @@ -156,25 +154,25 @@ public class AttributeCollectionTest { @Test public void testGetListTypeAndDelete() throws OseeCoreException { List<Attribute<Object>> list1 = - attributeCollection.getAttributeList(CoreAttributeTypes.Annotation, DeletionFlag.INCLUDE_DELETED); + attributeCollection.getList(CoreAttributeTypes.Annotation, DeletionFlag.INCLUDE_DELETED); assertEquals(1, list1.size()); assertTrue(list1.contains(deletedAttr)); List<Attribute<Object>> list2 = - attributeCollection.getAttributeList(CoreAttributeTypes.Annotation, DeletionFlag.EXCLUDE_DELETED); + attributeCollection.getList(CoreAttributeTypes.Annotation, DeletionFlag.EXCLUDE_DELETED); assertEquals(0, list2.size()); } @Test public void testGetSetTypeAndDelete() throws OseeCoreException { ResultSet<Attribute<Object>> set1 = - attributeCollection.getAttributeSet(CoreAttributeTypes.Annotation, DeletionFlag.INCLUDE_DELETED); + attributeCollection.getResultSet(CoreAttributeTypes.Annotation, DeletionFlag.INCLUDE_DELETED); assertEquals(1, set1.size()); checkContains(set1, deletedAttr, true); ResultSet<Attribute<Object>> set2 = - attributeCollection.getAttributeSet(CoreAttributeTypes.Annotation, DeletionFlag.EXCLUDE_DELETED); + attributeCollection.getResultSet(CoreAttributeTypes.Annotation, DeletionFlag.EXCLUDE_DELETED); assertEquals(0, set2.size()); } @@ -196,14 +194,8 @@ public class AttributeCollectionTest { Assert.assertEquals(cleanAttr, set.getExactlyOne()); } - private void checkContains(Iterable<Attribute<Object>> items, final Attribute<Object> toFind, boolean findExpected) { - Optional<Attribute<Object>> matched = Iterables.tryFind(items, new Predicate<Attribute<Object>>() { - @Override - public boolean apply(Attribute<Object> entry) { - return toFind.equals(entry); - } - }); - assertEquals(findExpected, matched.isPresent()); + private void checkContains(Iterable<? extends Attribute<?>> items, Attribute<Object> toFind, boolean findExpected) { + boolean actual = Iterables.contains(items, toFind); + assertEquals(findExpected, actual); } - } diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDeletedFilterTest.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDeletedFilterTest.java deleted file mode 100644 index 9fe2e650a80..00000000000 --- a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDeletedFilterTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012 Boeing. - * 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: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.orcs.core.internal.attribute; - -import static org.mockito.Mockito.when; -import org.eclipse.osee.framework.core.enums.DeletionFlag; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -/** - * Test Case for {@link AttributeDeletedFilter} - * - * @author Roberto E. Escobar - */ -@SuppressWarnings("rawtypes") -public class AttributeDeletedFilterTest { - - // @formatter:off - @Mock private Attribute deletedAttr; - @Mock private Attribute attribute; - // @formatter:on - - @Before - public void init() { - MockitoAnnotations.initMocks(this); - - when(deletedAttr.isDeleted()).thenReturn(true); - when(attribute.isDeleted()).thenReturn(false); - } - - @Test - public void testIncludeDeleted() throws OseeCoreException { - Assert.assertTrue(filter(DeletionFlag.INCLUDE_DELETED).accept(deletedAttr)); - Assert.assertTrue(filter(DeletionFlag.INCLUDE_DELETED).accept(attribute)); - } - - @Test - public void testExcludeDeleted() throws OseeCoreException { - Assert.assertFalse(filter(DeletionFlag.EXCLUDE_DELETED).accept(deletedAttr)); - Assert.assertTrue(filter(DeletionFlag.EXCLUDE_DELETED).accept(attribute)); - } - - private AttributeFilter filter(DeletionFlag flag) { - return new AttributeDeletedFilter(flag); - } - -} diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDirtyFilterTest.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDirtyFilterTest.java deleted file mode 100644 index ca450778a43..00000000000 --- a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDirtyFilterTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012 Boeing. - * 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: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.orcs.core.internal.attribute; - -import static org.mockito.Mockito.when; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.eclipse.osee.orcs.core.internal.attribute.AttributeDirtyFilter.DirtyFlag; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -/** - * Test Case for {@link AttributeDirtyFilter} - * - * @author Roberto E. Escobar - */ -@SuppressWarnings("rawtypes") -public class AttributeDirtyFilterTest { - - // @formatter:off - @Mock private Attribute dirtyAttr; - @Mock private Attribute noneDirtyAttr; - // @formatter:on - - @Before - public void init() { - MockitoAnnotations.initMocks(this); - - when(dirtyAttr.isDirty()).thenReturn(true); - when(noneDirtyAttr.isDirty()).thenReturn(false); - } - - @Test - public void testAcceptDirties() throws OseeCoreException { - Assert.assertFalse(filter(DirtyFlag.DIRTY).accept(noneDirtyAttr)); - Assert.assertTrue(filter(DirtyFlag.DIRTY).accept(dirtyAttr)); - } - - @Test - public void testAcceptNoneDirties() throws OseeCoreException { - Assert.assertTrue(filter(DirtyFlag.NON_DIRTY).accept(noneDirtyAttr)); - Assert.assertFalse(filter(DirtyFlag.NON_DIRTY).accept(dirtyAttr)); - } - - private AttributeFilter filter(DirtyFlag flag) { - return new AttributeDirtyFilter(flag); - } - -} diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFilterTest.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFilterTest.java deleted file mode 100644 index 6587b323c82..00000000000 --- a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFilterTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012 Boeing. - * 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: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.orcs.core.internal.attribute; - -import static org.mockito.Mockito.when; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; - -/** - * Test Case for {@link AttributeDeletedFilter} - * - * @author Roberto E. Escobar - */ -public class AttributeFilterTest { - - // @formatter:off - @SuppressWarnings("rawtypes") - @Mock private Attribute attribute; - // @formatter:on - - private AttributeFilter filter1; - private AttributeFilter filter2; - - @Before - public void init() { - MockitoAnnotations.initMocks(this); - filter1 = Mockito.spy(new FilterMock()); - filter2 = Mockito.spy(new FilterMock()); - } - - @Test - public void testAnd() throws OseeCoreException { - AttributeFilter andFilter = filter1.and(filter2); - - when(filter1.accept(attribute)).thenReturn(true); - when(filter2.accept(attribute)).thenReturn(true); - Assert.assertTrue(andFilter.accept(attribute)); - - when(filter1.accept(attribute)).thenReturn(false); - when(filter2.accept(attribute)).thenReturn(true); - Assert.assertFalse(andFilter.accept(attribute)); - - when(filter1.accept(attribute)).thenReturn(true); - when(filter2.accept(attribute)).thenReturn(false); - Assert.assertFalse(andFilter.accept(attribute)); - - when(filter1.accept(attribute)).thenReturn(false); - when(filter2.accept(attribute)).thenReturn(false); - Assert.assertFalse(andFilter.accept(attribute)); - } - - @Test - public void testOr() throws OseeCoreException { - AttributeFilter orFilter = filter1.or(filter2); - - when(filter1.accept(attribute)).thenReturn(true); - when(filter2.accept(attribute)).thenReturn(true); - Assert.assertTrue(orFilter.accept(attribute)); - - when(filter1.accept(attribute)).thenReturn(false); - when(filter2.accept(attribute)).thenReturn(true); - Assert.assertTrue(orFilter.accept(attribute)); - - when(filter1.accept(attribute)).thenReturn(true); - when(filter2.accept(attribute)).thenReturn(false); - Assert.assertTrue(orFilter.accept(attribute)); - - when(filter1.accept(attribute)).thenReturn(false); - when(filter2.accept(attribute)).thenReturn(false); - Assert.assertFalse(orFilter.accept(attribute)); - } - - public class FilterMock extends AttributeFilter { - @Override - public boolean accept(Attribute<?> attribute) { - return false; - } - }; - -} diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFromStringFilterTest.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFromStringFilterTest.java deleted file mode 100644 index f3c237ef2b4..00000000000 --- a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFromStringFilterTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012 Boeing. - * 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: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.orcs.core.internal.attribute; - -import static org.mockito.Mockito.when; -import java.util.Date; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -/** - * Test Case for {@link AttributeFromStringFilter} - * - * @author Roberto E. Escobar - */ -@SuppressWarnings("rawtypes") -public class AttributeFromStringFilterTest { - - // @formatter:off - @Mock private Attribute attribute1; - @Mock private Attribute attribute2; - @Mock private Attribute attribute3; - // @formatter:on - - private Date date; - - @Before - public void init() throws OseeCoreException { - MockitoAnnotations.initMocks(this); - - date = new Date(); - - when(attribute1.getValue()).thenReturn(45789L); - when(attribute2.getValue()).thenReturn(true); - when(attribute3.getValue()).thenReturn(date); - } - - @Test - public void testAccept() throws OseeCoreException { - Assert.assertTrue(filter("45789").accept(attribute1)); - Assert.assertTrue(filter("true").accept(attribute2)); - Assert.assertTrue(filter(date.toString()).accept(attribute3)); - } - - @Test - public void testNotAccept() throws OseeCoreException { - Assert.assertFalse(filter("Helo").accept(attribute1)); - Assert.assertFalse(filter("Hello").accept(attribute2)); - - Date date2 = new Date(123123111231L); - - Assert.assertFalse(filter(date2.toString()).accept(attribute3)); - - Assert.assertFalse(filter("true").accept(attribute1)); - Assert.assertFalse(filter("false").accept(attribute1)); - Assert.assertFalse(filter("false").accept(attribute2)); - } - - private <T> AttributeFilter filter(String value) { - return new AttributeFromStringFilter(value); - } - -} diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeTestSuite.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeTestSuite.java index 11115d3ab10..bce1bec5b35 100644 --- a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeTestSuite.java +++ b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeTestSuite.java @@ -20,13 +20,8 @@ import org.junit.runners.Suite; @Suite.SuiteClasses({ AttributeClassResolverTest.class, AttributeCollectionTest.class, - AttributeDeletedFilterTest.class, - AttributeDirtyFilterTest.class, AttributeFactoryTest.class, - AttributeFilterTest.class, - AttributeFromStringFilterTest.class, AttributeResultSetTest.class, - AttributeValueFilterTest.class, ArtifactTest.class}) public class AttributeTestSuite { // Test Suite diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeValueFilterTest.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeValueFilterTest.java deleted file mode 100644 index aa2c64ccd63..00000000000 --- a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeValueFilterTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012 Boeing. - * 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: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.orcs.core.internal.attribute; - -import static org.mockito.Mockito.when; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -/** - * Test Case for {@link AttributeValueFilter} - * - * @author Roberto E. Escobar - */ -@SuppressWarnings("rawtypes") -public class AttributeValueFilterTest { - - // @formatter:off - @Mock private Attribute attribute1; - @Mock private Attribute attribute2; - // @formatter:on - - @Before - public void init() throws OseeCoreException { - MockitoAnnotations.initMocks(this); - - when(attribute1.getValue()).thenReturn("Hello"); - when(attribute2.getValue()).thenReturn(true); - } - - @Test - public void testAccept() throws OseeCoreException { - Assert.assertTrue(filter("Hello").accept(attribute1)); - Assert.assertTrue(filter(true).accept(attribute2)); - } - - @Test - public void testNotAccept() throws OseeCoreException { - Assert.assertFalse(filter("Helo").accept(attribute1)); - Assert.assertFalse(filter("Hello").accept(attribute2)); - - Assert.assertFalse(filter(true).accept(attribute1)); - Assert.assertFalse(filter(false).accept(attribute1)); - Assert.assertFalse(filter(false).accept(attribute2)); - } - - private <T> AttributeFilter filter(T value) { - return new AttributeValueFilter<T>(value); - } - -} diff --git a/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/util/OrcsPredicatesTest.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/util/OrcsPredicatesTest.java new file mode 100644 index 00000000000..57f6556c71f --- /dev/null +++ b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/util/OrcsPredicatesTest.java @@ -0,0 +1,183 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.orcs.core.internal.util; + +import static org.eclipse.osee.framework.core.enums.DeletionFlag.EXCLUDE_DELETED; +import static org.eclipse.osee.framework.core.enums.DeletionFlag.INCLUDE_DELETED; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.attributeContainsPattern; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.attributeStringEquals; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.attributeValueEquals; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.deletionFlagEquals; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.excludeDeleted; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.includeDeleted; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.isDirty; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.isNotDirty; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; +import java.util.Date; +import java.util.regex.Pattern; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.orcs.core.internal.attribute.Attribute; +import org.eclipse.osee.orcs.data.HasDeleteState; +import org.eclipse.osee.orcs.data.Modifiable; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import com.google.common.base.Predicate; + +/** + * Test Case for {@link OrcsPredicates} + * + * @author Roberto E. Escobar + */ +public class OrcsPredicatesTest { + + // @formatter:off + @Mock private Modifiable dirty; + @Mock private Modifiable notDirty; + + @Mock private HasDeleteState deleted; + @Mock private HasDeleteState notDeleted; + + @SuppressWarnings("rawtypes") + @Mock private Attribute attribute1; + @SuppressWarnings("rawtypes") + @Mock private Attribute attribute2; + @SuppressWarnings("rawtypes") + @Mock private Attribute attribute3; + @SuppressWarnings("rawtypes") + @Mock private Attribute attribute4; + @SuppressWarnings("rawtypes") + @Mock private Attribute attribute5; + // @formatter:on + + private Date date; + + @Before + public void init() throws OseeCoreException { + MockitoAnnotations.initMocks(this); + + when(dirty.isDirty()).thenReturn(true); + when(notDirty.isDirty()).thenReturn(false); + + when(deleted.isDeleted()).thenReturn(true); + when(notDeleted.isDeleted()).thenReturn(false); + + date = new Date(); + + when(attribute1.getValue()).thenReturn(45789L); + when(attribute2.getValue()).thenReturn(true); + when(attribute3.getValue()).thenReturn(date); + when(attribute4.getValue()).thenReturn("Hello"); + when(attribute5.getValue()).thenReturn(true); + } + + @Test + public void testAcceptDirties() { + assertFalse(isDirty().apply(notDirty)); + assertTrue(isDirty().apply(dirty)); + } + + @Test + public void testAcceptNoneDirties() { + assertTrue(isNotDirty().apply(notDirty)); + assertFalse(isNotDirty().apply(dirty)); + } + + @Test + public void testIncludeDeleted() { + assertTrue(includeDeleted().apply(deleted)); + assertTrue(includeDeleted().apply(notDeleted)); + } + + @Test + public void testExcludeDeleted() { + assertFalse(excludeDeleted().apply(deleted)); + assertTrue(excludeDeleted().apply(notDeleted)); + } + + @Test + public void testDeletionFlag() { + when(attribute1.isDeleted()).thenReturn(true); + when(attribute2.isDeleted()).thenReturn(false); + + assertFalse(deletionFlagEquals(EXCLUDE_DELETED).apply(attribute1)); + assertTrue(deletionFlagEquals(EXCLUDE_DELETED).apply(attribute2)); + + assertTrue(deletionFlagEquals(INCLUDE_DELETED).apply(attribute1)); + assertTrue(deletionFlagEquals(INCLUDE_DELETED).apply(attribute2)); + } + + @SuppressWarnings("unchecked") + @Test + public void testAttributeStringEquals() throws OseeCoreException { + assertTrue(attributeStringEquals("45789").apply(attribute1)); + assertTrue(attributeStringEquals("true").apply(attribute2)); + assertTrue(attributeStringEquals(date.toString()).apply(attribute3)); + + assertFalse(attributeStringEquals("Helo").apply(attribute1)); + assertFalse(attributeStringEquals("Hello").apply(attribute2)); + + Date date2 = new Date(123123111231L); + + assertFalse(attributeStringEquals(date2.toString()).apply(attribute3)); + + assertFalse(attributeStringEquals("true").apply(attribute1)); + assertFalse(attributeStringEquals("false").apply(attribute1)); + assertFalse(attributeStringEquals("false").apply(attribute2)); + + when(attribute1.getValue()).thenReturn((String) null); + + assertTrue(attributeStringEquals(null).apply(attribute1)); + assertFalse(attributeStringEquals("null").apply(attribute1)); + } + + @SuppressWarnings("unchecked") + @Test + public void testAttributeValueEquals() { + assertTrue(attributeValueEquals("Hello").apply(attribute4)); + assertTrue(attributeValueEquals(true).apply(attribute5)); + + assertFalse(attributeValueEquals("Helo").apply(attribute4)); + assertFalse(attributeValueEquals("Hello").apply(attribute5)); + + assertFalse(attributeValueEquals(true).apply(attribute4)); + assertFalse(attributeValueEquals(false).apply(attribute4)); + assertFalse(attributeValueEquals(false).apply(attribute5)); + } + + @SuppressWarnings("unchecked") + @Test + public void testAttributeContainsPattern() throws OseeCoreException { + when(attribute1.getValue()).thenReturn("123-456-7890", "00-000-0000", "000-000-0000"); + + Predicate<Attribute<CharSequence>> telAttribute = attributeContainsPattern("[0-9]{3}[-][0-9]{3}[-][0-9]{4}"); + + assertTrue(telAttribute.apply(attribute1)); + assertFalse(telAttribute.apply(attribute1)); + assertTrue(telAttribute.apply(attribute1)); + } + + @SuppressWarnings("unchecked") + @Test + public void testAttributeContainsPattern2() throws OseeCoreException { + Pattern pattern = Pattern.compile("[0-9]{3}[-][0-9]{3}[-][0-9]{4}"); + when(attribute1.getValue()).thenReturn("123-456-7890", "00-000-0000", "000-000-0000"); + + Predicate<Attribute<CharSequence>> telAttribute = attributeContainsPattern(pattern); + + assertTrue(telAttribute.apply(attribute1)); + assertFalse(telAttribute.apply(attribute1)); + assertTrue(telAttribute.apply(attribute1)); + } +} diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeValueFilter.java b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/util/UtilTestSuite.java index 87bdcbd8b6e..dd7e6b7f827 100644 --- a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeValueFilter.java +++ b/plugins/org.eclipse.osee.orcs.core.test/src/org/eclipse/osee/orcs/core/internal/util/UtilTestSuite.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Boeing. + * Copyright (c) 2013 Boeing. * 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 @@ -8,22 +8,16 @@ * Contributors: * Boeing - initial API and implementation *******************************************************************************/ -package org.eclipse.osee.orcs.core.internal.attribute; +package org.eclipse.osee.orcs.core.internal.util; -import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; /** * @author Roberto E. Escobar */ -public class AttributeValueFilter<T> extends AttributeFilter { - private final T toMatch; - - public AttributeValueFilter(T value) { - toMatch = value; - } - - @Override - public boolean accept(Attribute<?> attribute) throws OseeCoreException { - return attribute.getValue().equals(toMatch); - } +@RunWith(Suite.class) +@Suite.SuiteClasses({OrcsPredicatesTest.class}) +public class UtilTestSuite { + // Test Suite } diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeCollection.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeCollection.java index dc767475d10..6f20f6a9288 100644 --- a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeCollection.java +++ b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeCollection.java @@ -10,30 +10,22 @@ *******************************************************************************/ package org.eclipse.osee.orcs.core.internal.attribute; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashSet; -import java.util.LinkedList; +import static com.google.common.base.Predicates.and; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.attributeStringEquals; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.attributeValueEquals; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.deletionFlagEquals; import java.util.List; import org.eclipse.osee.framework.core.data.IAttributeType; import org.eclipse.osee.framework.core.data.ResultSet; import org.eclipse.osee.framework.core.enums.DeletionFlag; import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.eclipse.osee.framework.jdk.core.type.HashCollection; -import org.eclipse.osee.orcs.core.internal.attribute.AttributeDirtyFilter.DirtyFlag; +import org.eclipse.osee.orcs.core.internal.util.AbstractTypeCollection; +import com.google.common.base.Predicate; /** * @author Roberto E. Escobar */ -public class AttributeCollection { - - private static final AttributeFilter FILTER_NONE_DIRTY = new AttributeDirtyFilter(DirtyFlag.DIRTY); - private static final AttributeFilter INCLUDE_DELETED = new AttributeDeletedFilter(DeletionFlag.INCLUDE_DELETED); - private static final AttributeFilter EXCLUDE_DELETED = new AttributeDeletedFilter(DeletionFlag.EXCLUDE_DELETED); - - private final HashCollection<IAttributeType, Attribute<?>> attributes = - new HashCollection<IAttributeType, Attribute<?>>(false, LinkedHashSet.class, 12); +public class AttributeCollection extends AbstractTypeCollection<IAttributeType, Attribute<?>, IAttributeType, Attribute<?>> { private final AttributeExceptionFactory exceptionFactory; @@ -42,113 +34,55 @@ public class AttributeCollection { this.exceptionFactory = exceptionFactory; } - public void addAttribute(IAttributeType type, Attribute<?> attribute) { - attributes.put(type, attribute); - } - - public void removeAttribute(IAttributeType attributeType, Attribute<?> attribute) { - attributes.removeValue(attributeType, attribute); - } - - public List<Attribute<?>> getAllAttributes() { - return attributes.getValues(); - } + ////////////////////////////////////////////////////////////// - public Collection<IAttributeType> getExistingTypes(DeletionFlag includeDeleted) throws OseeCoreException { - List<IAttributeType> toReturn = new ArrayList<IAttributeType>(); - for (Attribute<?> attribute : getAttributeList(includeDeleted)) { - toReturn.add(attribute.getAttributeType()); - } - return toReturn; + @Override + @SuppressWarnings({"unchecked", "rawtypes"}) + protected ResultSet<Attribute<?>> createResultSet(List<Attribute<?>> values) { + return new AttributeResultSet(exceptionFactory, values); } - public List<Attribute<Object>> getAttributeListDirties() throws OseeCoreException { - return getListByFilter(attributes.getValues(), FILTER_NONE_DIRTY); + @Override + @SuppressWarnings({"unchecked", "rawtypes"}) + protected <T extends Attribute<?>> ResultSet<T> createResultSet(IAttributeType attributeType, List<T> values) { + return new AttributeResultSet(exceptionFactory, attributeType, values); } - public boolean hasAttributesDirty() { - return hasItemMatchingFilter(attributes.getValues(), FILTER_NONE_DIRTY); + @Override + protected Attribute<?> asMatcherData(Attribute<?> data) { + return data; } - ////////////////////////////////////////////////////////////// - - public <T> ResultSet<Attribute<T>> getAttributeSet(IAttributeType attributeType, DeletionFlag includeDeleted) throws OseeCoreException { - List<Attribute<T>> result = getListByFilter(attributes.getValues(attributeType), includeDeleted); - return new AttributeResultSet<T>(exceptionFactory, attributeType, result); + @Override + protected IAttributeType getType(Attribute<?> data) throws OseeCoreException { + return data.getAttributeType(); } - public <T> ResultSet<Attribute<T>> getAttributeSet(DeletionFlag includeDeleted) throws OseeCoreException { - return getSetByFilter(attributes.getValues(), includeDeleted); + public <T> ResultSet<Attribute<T>> getResultSet(IAttributeType attributeType, DeletionFlag includeDeleted) throws OseeCoreException { + List<Attribute<T>> result = getList(attributeType, includeDeleted); + return createResultSet(attributeType, result); } + @SuppressWarnings({"unchecked", "rawtypes"}) public <T> ResultSet<Attribute<T>> getAttributeSetFromString(IAttributeType attributeType, DeletionFlag includeDeleted, String value) throws OseeCoreException { - AttributeFilter filter = new AttributeDeletedFilter(includeDeleted); - filter = filter.and(new AttributeFromStringFilter(value)); - return getSetByFilter(attributes.getValues(attributeType), filter); + Predicate deleteStateMatch = deletionFlagEquals(includeDeleted); + Predicate stringEqualsMatch = attributeStringEquals(value); + Predicate filter = and(deleteStateMatch, stringEqualsMatch); + return getSetByFilter(attributeType, filter); } + @SuppressWarnings({"unchecked", "rawtypes"}) public <T> ResultSet<Attribute<T>> getAttributeSetFromValue(IAttributeType attributeType, DeletionFlag includeDeleted, T value) throws OseeCoreException { - AttributeFilter filter = new AttributeDeletedFilter(includeDeleted); - filter = filter.and(new AttributeValueFilter<T>(value)); - return getSetByFilter(attributes.getValues(attributeType), filter); - } - - private <T> ResultSet<Attribute<T>> getSetByFilter(Collection<Attribute<?>> source, DeletionFlag includeDeleted) throws OseeCoreException { - return getSetByFilter(source, DeletionFlag.INCLUDE_DELETED == includeDeleted ? INCLUDE_DELETED : EXCLUDE_DELETED); - } - - ////////////////////////////////////////////////////////////// - - public <T> List<Attribute<T>> getAttributeList(IAttributeType attributeType, DeletionFlag includeDeleted) throws OseeCoreException { - return getListByFilter(attributes.getValues(attributeType), includeDeleted); - } - - public <T> List<Attribute<T>> getAttributeList(DeletionFlag includeDeleted) throws OseeCoreException { - return getListByFilter(attributes.getValues(), includeDeleted); - } - - private <T> List<Attribute<T>> getListByFilter(Collection<Attribute<?>> source, DeletionFlag includeDeleted) throws OseeCoreException { - return getListByFilter(source, DeletionFlag.INCLUDE_DELETED == includeDeleted ? INCLUDE_DELETED : EXCLUDE_DELETED); - } - - ////////////////////////////////////////////////////////////// - - private <T> AttributeResultSet<T> getSetByFilter(Collection<Attribute<?>> source, AttributeFilter filter) throws OseeCoreException { - List<Attribute<T>> values = getListByFilter(source, filter); - return new AttributeResultSet<T>(exceptionFactory, values); - } - - @SuppressWarnings("unchecked") - private <T> List<Attribute<T>> getListByFilter(Collection<Attribute<?>> source, AttributeFilter filter) throws OseeCoreException { - List<Attribute<T>> toReturn; - if (source != null && !source.isEmpty()) { - toReturn = new LinkedList<Attribute<T>>(); - for (Attribute<?> attribute : source) { - if (filter.accept(attribute)) { - toReturn.add((Attribute<T>) attribute); - } - } - } else { - toReturn = Collections.emptyList(); - } - return toReturn; + Predicate deleteStateMatch = deletionFlagEquals(includeDeleted); + Predicate attributeValueEquals = attributeValueEquals(value); + Predicate filter = and(attributeValueEquals, deleteStateMatch); + return getSetByFilter(attributeType, filter); } - private boolean hasItemMatchingFilter(Collection<Attribute<?>> source, AttributeFilter filter) { - boolean result = false; - if (source != null && !source.isEmpty()) { - for (Attribute<?> attribute : source) { - try { - if (filter.accept(attribute)) { - result = true; - break; - } - } catch (OseeCoreException ex) { - // do nothing - } - } - } - return result; + @SuppressWarnings({"unchecked", "rawtypes"}) + public <T> List<Attribute<T>> getList(IAttributeType attributeType, DeletionFlag includeDeleted) throws OseeCoreException { + Predicate attributeDeletionFlagEquals = deletionFlagEquals(includeDeleted); + return getListByFilter(attributeType, attributeDeletionFlagEquals); } } diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDeletedFilter.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDeletedFilter.java deleted file mode 100644 index 212388e109f..00000000000 --- a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDeletedFilter.java +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012 Boeing. - * 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: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.orcs.core.internal.attribute; - -import org.eclipse.osee.framework.core.enums.DeletionFlag; - -/** - * @author Roberto E. Escobar - */ -public class AttributeDeletedFilter extends AttributeFilter { - - private final boolean checkNeeded; - - public AttributeDeletedFilter(DeletionFlag includeDeleted) { - this.checkNeeded = !includeDeleted.areDeletedAllowed(); - } - - @Override - public boolean accept(Attribute<?> attribute) { - boolean result = true; - if (checkNeeded) { - result = !attribute.isDeleted(); - } - return result; - } -} diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDirtyFilter.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDirtyFilter.java deleted file mode 100644 index d296c31db37..00000000000 --- a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeDirtyFilter.java +++ /dev/null @@ -1,39 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012 Boeing. - * 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: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.orcs.core.internal.attribute; - -/** - * @author Roberto E. Escobar - */ -public class AttributeDirtyFilter extends AttributeFilter { - - public static enum DirtyFlag { - DIRTY, - NON_DIRTY, - } - - private final DirtyFlag dirtyFlag; - - public AttributeDirtyFilter(DirtyFlag includeDirty) { - this.dirtyFlag = includeDirty; - } - - @Override - public boolean accept(Attribute<?> attribute) { - boolean result = true; - if (dirtyFlag == DirtyFlag.DIRTY) { - result = attribute.isDirty(); - } else if (dirtyFlag == DirtyFlag.NON_DIRTY) { - result = !attribute.isDirty(); - } - return result; - } -} diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFilter.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFilter.java deleted file mode 100644 index e38e9a2f869..00000000000 --- a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFilter.java +++ /dev/null @@ -1,49 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012 Boeing. - * 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: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.orcs.core.internal.attribute; - -import org.eclipse.osee.framework.core.exception.OseeCoreException; - -/** - * @author Roberto E. Escobar - */ -public abstract class AttributeFilter { - - public abstract boolean accept(Attribute<?> attribute) throws OseeCoreException; - - private abstract static class BooleanOperation extends AttributeFilter { - protected final AttributeFilter filter1; - protected final AttributeFilter filter2; - - public BooleanOperation(AttributeFilter filter1, AttributeFilter filter2) { - this.filter1 = filter1; - this.filter2 = filter2; - } - } - - public AttributeFilter or(AttributeFilter anotherFilter) { - return new BooleanOperation(this, anotherFilter) { - @Override - public boolean accept(Attribute<?> attribute) throws OseeCoreException { - return filter1.accept(attribute) || filter2.accept(attribute); - } - }; - } - - public AttributeFilter and(AttributeFilter anotherFilter) { - return new BooleanOperation(this, anotherFilter) { - @Override - public boolean accept(Attribute<?> attribute) throws OseeCoreException { - return filter1.accept(attribute) && filter2.accept(attribute); - } - }; - } -}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFromStringFilter.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFromStringFilter.java deleted file mode 100644 index 0003c47054e..00000000000 --- a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeFromStringFilter.java +++ /dev/null @@ -1,29 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012 Boeing. - * 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: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.orcs.core.internal.attribute; - -import org.eclipse.osee.framework.core.exception.OseeCoreException; - -/** - * @author Roberto E. Escobar - */ -public class AttributeFromStringFilter extends AttributeFilter { - private final String toMatch; - - public AttributeFromStringFilter(String value) { - toMatch = value; - } - - @Override - public boolean accept(Attribute<?> attribute) throws OseeCoreException { - return toMatch.equals(String.valueOf(attribute.getValue())); - } -} diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeManager.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeManager.java index 047e38616ba..954f51e3f22 100644 --- a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeManager.java +++ b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeManager.java @@ -45,7 +45,7 @@ public interface AttributeManager extends Identifiable, AttributesWriteable, Has int getMinimumAttributeTypeAllowed(IAttributeType attributeType) throws OseeCoreException; - List<Attribute<Object>> getAttributesDirty() throws OseeCoreException; + List<Attribute<?>> getAttributesDirty() throws OseeCoreException; void deleteAttributesByArtifact() throws OseeCoreException; diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeManagerImpl.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeManagerImpl.java index 4366230a748..29e9191aa46 100644 --- a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeManagerImpl.java +++ b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/attribute/AttributeManagerImpl.java @@ -55,19 +55,19 @@ public abstract class AttributeManagerImpl extends AbstractIdentity<String> impl this.attributes = new AttributeCollection(this); } - protected List<Attribute<?>> getAllAttributes() { - return attributes.getAllAttributes(); + protected Collection<Attribute<?>> getAllAttributes() { + return attributes.getAll(); } @Override public synchronized void add(IAttributeType type, Attribute<? extends Object> attribute) { - attributes.addAttribute(type, attribute); + attributes.add(type, attribute); attribute.getOrcsData().setArtifactId(getLocalId()); } @Override - public void remove(IAttributeType type, Attribute<? extends Object> attribute) { - attributes.removeAttribute(type, attribute); + public synchronized void remove(IAttributeType type, Attribute<? extends Object> attribute) { + attributes.remove(type, attribute); attribute.getOrcsData().setArtifactId(-1); } @@ -85,8 +85,8 @@ public abstract class AttributeManagerImpl extends AbstractIdentity<String> impl } @Override - public List<Attribute<Object>> getAttributesDirty() throws OseeCoreException { - return attributes.getAttributeListDirties(); + public List<Attribute<?>> getAttributesDirty() throws OseeCoreException { + return attributes.getDirties(); } @Override @@ -98,7 +98,7 @@ public abstract class AttributeManagerImpl extends AbstractIdentity<String> impl @Override public boolean areAttributesDirty() { - return attributes.hasAttributesDirty(); + return attributes.hasDirty(); } @Override @@ -143,8 +143,7 @@ public abstract class AttributeManagerImpl extends AbstractIdentity<String> impl @Override public List<AttributeReadable<Object>> getAttributes() throws OseeCoreException { - - List<Attribute<Object>> items = getAttributesExcludeDeleted(); + List<Attribute<?>> items = getAttributesExcludeDeleted(); return Collections.castAll(items); } @@ -175,7 +174,7 @@ public abstract class AttributeManagerImpl extends AbstractIdentity<String> impl @Override public List<AttributeReadable<Object>> getAttributes(DeletionFlag includeDeleted) throws OseeCoreException { - List<Attribute<Object>> items = getAttributesHelper(includeDeleted); + List<Attribute<?>> items = getAttributesHelper(includeDeleted); return Collections.castAll(items); } @@ -186,8 +185,8 @@ public abstract class AttributeManagerImpl extends AbstractIdentity<String> impl } @Override - public <T> List<AttributeWriteable<T>> getWriteableAttributes() throws OseeCoreException { - List<Attribute<T>> items = getAttributesExcludeDeleted(); + public List<AttributeWriteable<Object>> getWriteableAttributes() throws OseeCoreException { + List<Attribute<?>> items = getAttributesExcludeDeleted(); return Collections.castAll(items); } @@ -353,7 +352,7 @@ public abstract class AttributeManagerImpl extends AbstractIdentity<String> impl } private <T> Attribute<T> getOrCreateSoleAttribute(IAttributeType attributeType) throws OseeCoreException { - ResultSet<Attribute<T>> result = attributes.getAttributeSet(attributeType, DeletionFlag.EXCLUDE_DELETED); + ResultSet<Attribute<T>> result = attributes.getResultSet(attributeType, DeletionFlag.EXCLUDE_DELETED); Attribute<T> attribute = result.getAtMostOneOrNull(); if (attribute == null) { attribute = internalCreateAttributeHelper(attributeType); @@ -363,36 +362,36 @@ public abstract class AttributeManagerImpl extends AbstractIdentity<String> impl private <T> Attribute<T> getSoleAttribute(IAttributeType attributeType) throws OseeCoreException { ensureAttributesLoaded(); - ResultSet<Attribute<T>> result = attributes.getAttributeSet(attributeType, DeletionFlag.EXCLUDE_DELETED); + ResultSet<Attribute<T>> result = attributes.getResultSet(attributeType, DeletionFlag.EXCLUDE_DELETED); return result.getExactlyOne(); } ////////////////////////////////////////////////////////////// - private <T> List<Attribute<T>> getAttributesExcludeDeleted() throws OseeCoreException { + private List<Attribute<?>> getAttributesExcludeDeleted() throws OseeCoreException { return getAttributesHelper(DeletionFlag.EXCLUDE_DELETED); } - private <T> List<Attribute<T>> getAttributesExcludeDeleted(IAttributeType attributeType) throws OseeCoreException { - return getAttributesHelper(attributeType, DeletionFlag.EXCLUDE_DELETED); + private List<Attribute<?>> getAttributesIncludeDeleted() throws OseeCoreException { + return getAttributesHelper(DeletionFlag.INCLUDE_DELETED); } - private <T> List<Attribute<T>> getAttributesIncludeDeleted() throws OseeCoreException { - return getAttributesHelper(DeletionFlag.INCLUDE_DELETED); + private <T> List<Attribute<T>> getAttributesExcludeDeleted(IAttributeType attributeType) throws OseeCoreException { + return getAttributesHelper(attributeType, DeletionFlag.EXCLUDE_DELETED); } private <T> List<Attribute<T>> getAttributesIncludeDeleted(IAttributeType attributeType) throws OseeCoreException { return getAttributesHelper(attributeType, DeletionFlag.INCLUDE_DELETED); } - private <T> List<Attribute<T>> getAttributesHelper(DeletionFlag includeDeleted) throws OseeCoreException { + private List<Attribute<?>> getAttributesHelper(DeletionFlag includeDeleted) throws OseeCoreException { ensureAttributesLoaded(); - return attributes.getAttributeList(includeDeleted); + return attributes.getList(includeDeleted); } private <T> List<Attribute<T>> getAttributesHelper(IAttributeType attributeType, DeletionFlag includeDeleted) throws OseeCoreException { ensureAttributesLoaded(); - return attributes.getAttributeList(attributeType, includeDeleted); + return attributes.getList(attributeType, includeDeleted); } ////////////////////////////////////////////////////////////// diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/util/AbstractTypeCollection.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/util/AbstractTypeCollection.java new file mode 100644 index 00000000000..83219f6de8f --- /dev/null +++ b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/util/AbstractTypeCollection.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.orcs.core.internal.util; + +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.deletionFlagEquals; +import static org.eclipse.osee.orcs.core.internal.util.OrcsPredicates.isDirty; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import org.eclipse.osee.framework.core.data.ResultSet; +import org.eclipse.osee.framework.core.enums.DeletionFlag; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.orcs.data.OrcsWriteable; +import com.google.common.base.Predicate; +import com.google.common.collect.Multimap; + +/** + * @author Roberto E. Escobar + */ +public abstract class AbstractTypeCollection<TYPE, MATCH_DATA extends OrcsWriteable, KEY, DATA> extends FilterableCollection<MATCH_DATA, KEY, DATA> { + + protected AbstractTypeCollection(Multimap<KEY, DATA> map) { + super(map); + } + + protected AbstractTypeCollection() { + super(); + } + + protected abstract TYPE getType(DATA data) throws OseeCoreException; + + public Collection<? extends TYPE> getExistingTypes(DeletionFlag includeDeleted) throws OseeCoreException { + Set<TYPE> toReturn = new LinkedHashSet<TYPE>(); + for (DATA data : getList(includeDeleted)) { + if (isValid(data)) { + toReturn.add(getType(data)); + } + } + return toReturn; + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public List<DATA> getDirties() throws OseeCoreException { + Predicate matchDirties = isDirty(); + return getListByFilter(matchDirties); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public boolean hasDirty() { + Predicate matchDirties = isDirty(); + return hasItemMatchingFilter(matchDirties); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public List<DATA> getList(DeletionFlag includeDeleted) throws OseeCoreException { + Predicate deletedStateMatch = deletionFlagEquals(includeDeleted); + return getListByFilter(deletedStateMatch); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public ResultSet<DATA> getResultSet(DeletionFlag includeDeleted) throws OseeCoreException { + Predicate value = deletionFlagEquals(includeDeleted); + return getResultSetByFilter(value); + } + +} diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/util/FilterableCollection.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/util/FilterableCollection.java new file mode 100644 index 00000000000..ace8e62b655 --- /dev/null +++ b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/util/FilterableCollection.java @@ -0,0 +1,138 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.orcs.core.internal.util; + +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import org.eclipse.osee.framework.core.data.ResultSet; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import com.google.common.base.Predicate; +import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; + +/** + * @author Roberto E. Escobar + */ +public abstract class FilterableCollection<MATCH_DATA, KEY, DATA> { + + private final Multimap<KEY, DATA> map; + + protected FilterableCollection(Multimap<KEY, DATA> map) { + super(); + this.map = map; + } + + protected FilterableCollection() { + this(Multimaps.synchronizedMultimap(LinkedHashMultimap.<KEY, DATA> create())); + } + + protected abstract ResultSet<DATA> createResultSet(List<DATA> values); + + protected abstract <T extends DATA> ResultSet<T> createResultSet(KEY attributeType, List<T> values); + + public void add(KEY type, DATA data) { + map.put(type, data); + } + + public void remove(KEY type, DATA data) { + map.remove(type, data); + } + + public Collection<DATA> getAll() { + return map.values(); + } + + public Collection<DATA> getAllByType(KEY type) { + return map.get(type); + } + + protected List<DATA> getListByFilter(Predicate<MATCH_DATA> matcher) throws OseeCoreException { + return getListByFilter(getAll(), matcher); + } + + protected ResultSet<DATA> getResultSetByFilter(Predicate<MATCH_DATA> matcher) throws OseeCoreException { + return getResultSetByFilter(getAll(), matcher); + } + + protected <T extends DATA> ResultSet<T> getSetByFilter(KEY type, Predicate<MATCH_DATA> matcher) throws OseeCoreException { + List<T> result = getListByFilter(type, matcher); + ResultSet<T> resultSet = createResultSet(type, result); + return resultSet; + } + + protected <T extends DATA> List<T> getListByFilter(KEY type, Predicate<MATCH_DATA> matcher) throws OseeCoreException { + return getListByFilter(getAllByType(type), matcher); + } + + protected boolean hasItemMatchingFilter(Predicate<MATCH_DATA> matcher) { + return hasItemMatchingFilter(getAll(), matcher); + } + + protected boolean hasItemMatchingFilter(KEY type, Predicate<MATCH_DATA> matcher) { + return hasItemMatchingFilter(getAllByType(type), matcher); + } + + private ResultSet<DATA> getResultSetByFilter(Collection<DATA> source, Predicate<MATCH_DATA> matcher) throws OseeCoreException { + List<DATA> values = getListByFilter(source, matcher); + return createResultSet(values); + } + + @SuppressWarnings({"unchecked", "unused"}) + private <T extends DATA> List<T> getListByFilter(Collection<DATA> source, Predicate<MATCH_DATA> matcher) throws OseeCoreException { + List<T> toReturn; + if (source != null && !source.isEmpty()) { + toReturn = new LinkedList<T>(); + for (Iterator<DATA> iterator = source.iterator(); iterator.hasNext();) { + DATA data = iterator.next(); + if (isValid(data)) { + MATCH_DATA toMatch = asMatcherData(data); + if (matcher.apply(toMatch)) { + toReturn.add((T) data); + } + } else { + iterator.remove(); + } + } + } else { + toReturn = Collections.emptyList(); + } + return toReturn; + } + + private boolean hasItemMatchingFilter(Collection<DATA> source, Predicate<MATCH_DATA> matcher) { + boolean result = false; + if (source != null && !source.isEmpty()) { + for (Iterator<DATA> iterator = source.iterator(); iterator.hasNext();) { + DATA data = iterator.next(); + if (isValid(data)) { + MATCH_DATA toMatch = asMatcherData(data); + if (matcher.apply(toMatch)) { + result = true; + break; + } + } else { + iterator.remove(); + } + } + } + return result; + } + + protected boolean isValid(DATA data) { + return true; + } + + protected abstract MATCH_DATA asMatcherData(DATA data); +} diff --git a/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/util/OrcsPredicates.java b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/util/OrcsPredicates.java new file mode 100644 index 00000000000..ec3cf9f989a --- /dev/null +++ b/plugins/org.eclipse.osee.orcs.core/src/org/eclipse/osee/orcs/core/internal/util/OrcsPredicates.java @@ -0,0 +1,136 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.orcs.core.internal.util; + +import static com.google.common.base.Predicates.compose; +import static com.google.common.base.Predicates.contains; +import static com.google.common.base.Predicates.containsPattern; +import static com.google.common.base.Predicates.equalTo; +import static com.google.common.base.Predicates.not; +import java.util.regex.Pattern; +import org.eclipse.osee.framework.core.enums.DeletionFlag; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.orcs.core.internal.attribute.Attribute; +import org.eclipse.osee.orcs.data.HasDeleteState; +import org.eclipse.osee.orcs.data.Modifiable; +import com.google.common.base.Function; +import com.google.common.base.Predicate; + +/** + * @author Roberto E. Escobar + */ +public final class OrcsPredicates { + + private OrcsPredicates() { + // Utility class + } + + public static <T extends HasDeleteState> Predicate<T> includeDeleted() { + return deletionFlagEquals(DeletionFlag.INCLUDE_DELETED); + } + + public static <T extends HasDeleteState> Predicate<T> excludeDeleted() { + return deletionFlagEquals(DeletionFlag.EXCLUDE_DELETED); + } + + public static Predicate<Modifiable> isNotDirty() { + return not(isDirty()); + } + + public static <T extends Modifiable> Predicate<T> isDirty() { + return new Predicate<T>() { + + @Override + public boolean apply(T data) { + return data.isDirty(); + } + }; + } + + public static <T extends HasDeleteState> Predicate<T> deletionFlagEquals(DeletionFlag includeDeleted) { + return new DeletedMatcher<T>(includeDeleted); + } + + public static Predicate<Attribute<String>> attributeStringEquals(String target) { + return attributeString(equalTo(target)); + } + + public static <T> Predicate<Attribute<T>> attributeValueEquals(T target) { + return attributeValue(equalTo(target)); + } + + public static Predicate<Attribute<CharSequence>> attributeContainsPattern(String pattern) { + return attributeString(containsPattern(pattern)); + } + + public static Predicate<Attribute<CharSequence>> attributeContainsPattern(Pattern pattern) { + return attributeString(contains(pattern)); + } + + public static <T extends CharSequence> Predicate<Attribute<T>> attributeString(Predicate<T> predicate) { + return compose(predicate, new Function<Attribute<T>, T>() { + + @Override + public T apply(Attribute<T> input) { + T value = null; + try { + Object rawValue = input.getValue(); + if (rawValue != null) { + value = asString(rawValue); + } + } catch (OseeCoreException ex) { + // Do nothing; + } + return value; + } + + @SuppressWarnings("unchecked") + private T asString(Object rawValue) { + return (T) String.valueOf(rawValue); + } + }); + } + + public static <T> Predicate<Attribute<T>> attributeValue(Predicate<T> predicate) { + return compose(predicate, new Function<Attribute<T>, T>() { + + @Override + public T apply(Attribute<T> input) { + T value = null; + try { + value = input.getValue(); + } catch (OseeCoreException ex) { + // Do nothing; + } + return value; + } + + }); + } + + private static class DeletedMatcher<T extends HasDeleteState> implements Predicate<T> { + + private final boolean checkNeeded; + + public DeletedMatcher(DeletionFlag includeDeleted) { + this.checkNeeded = !includeDeleted.areDeletedAllowed(); + } + + @Override + public boolean apply(T data) { + boolean result = true; + if (checkNeeded) { + result = !data.isDeleted(); + } + return result; + } + } +} |