Author: rwesten
Date: Tue Feb 28 18:34:08 2012
New Revision: 1294763
URL: http://svn.apache.org/viewvc?rev=1294763&view=rev
Log:
implementation of STANBOL-513
Modified:
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItem.java
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/helper/ContentItemImpl.java
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/test/java/org/apache/stanbol/enhancer/serviceapi/helper/ContentItemTest.java
Modified:
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItem.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItem.java?rev=1294763&r1=1294762&r2=1294763&view=diff
==============================================================================
---
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItem.java
(original)
+++
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItem.java
Tue Feb 28 18:34:08 2012
@@ -114,4 +114,29 @@ public interface ContentItem {
* uriRef or object.
*/
Object addPart(UriRef uriRef, Object object);
+
+ /**
+ * Removes a part - other than the main content part - from this
ContentItem
+ * @param index the index of the part to remove. NOTE that index '0'
+ * - the main content part - can NOT be removed!
+ * @throws NoSuchPartException if no ContentPart with the parsed id exists
+ * @throws IllegalArgumentException it the parsed index < 0
+ * @throws IllegalStateException if '0' is parsed as index. The index '0'
+ * - the main content part - can NOT be removed!
+ */
+ void removePart(int index);
+
+ /**
+ * Removes a part - other than the main content part - from this
ContentItem
+ * @param uriRef the uri of the part to remove. NOTE that the part with the
+ * uri <code>{@link #getPartUri(int) getPartUri(0)}</code> - the main
+ * content part - can NOT be removed!
+ * @throws NoSuchPartException if no ContentPart with the parsed uri exists
+ * @throws IllegalArgumentException it the parsed uri is <code>null</code>
+ * @throws IllegalStateException if the parsed uri is equals to
+ * <code>{@link #getPartUri(int) getPartUri(0)}</code>. This uri refers to
+ * the main content part. This part can NOT be removed by this method
+ */
+ void removePart(UriRef uriRef);
+
}
Modified:
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/helper/ContentItemImpl.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/helper/ContentItemImpl.java?rev=1294763&r1=1294762&r2=1294763&view=diff
==============================================================================
---
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/helper/ContentItemImpl.java
(original)
+++
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/helper/ContentItemImpl.java
Tue Feb 28 18:34:08 2012
@@ -213,7 +213,42 @@ public abstract class ContentItemImpl im
writeLock.unlock();
}
}
-
+ @Override
+ public void removePart(int index) {
+ if(index < 0) {
+ throw new IllegalArgumentException("The parsed index MUST NOT
be < 0");
+ }
+ if(index == 0){
+ throw new IllegalStateException("The main ContentPart (index ==
0) CAN NOT be removed!");
+ }
+ writeLock.lock();
+ try {
+ UriRef partUri = getPartUri(index);
+ parts.remove(partUri);
+ } finally {
+ writeLock.unlock();
+ }
+ }
+ @Override
+ public void removePart(UriRef uriRef) {
+ if(uriRef == null){
+ throw new IllegalArgumentException("The parsed uriRef MUST NOT
be NULL!");
+ }
+ writeLock.lock();
+ try {
+ UriRef mainContentPartUri = parts.keySet().iterator().next();
+ if(uriRef.equals(mainContentPartUri)){
+ throw new IllegalStateException("The main ContentPart (uri '"
+ + uriRef+"') CAN NOT be removed!");
+ }
+ if(parts.remove(uriRef) == null){
+ throw new NoSuchPartException(uriRef);
+ }
+ } finally {
+ writeLock.unlock();
+ }
+ }
+
@Override
public UriRef getUri() {
return uri;
Modified:
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/test/java/org/apache/stanbol/enhancer/serviceapi/helper/ContentItemTest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/servicesapi/src/test/java/org/apache/stanbol/enhancer/serviceapi/helper/ContentItemTest.java?rev=1294763&r1=1294762&r2=1294763&view=diff
==============================================================================
---
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/test/java/org/apache/stanbol/enhancer/serviceapi/helper/ContentItemTest.java
(original)
+++
incubator/stanbol/trunk/enhancer/generic/servicesapi/src/test/java/org/apache/stanbol/enhancer/serviceapi/helper/ContentItemTest.java
Tue Feb 28 18:34:08 2012
@@ -97,4 +97,81 @@ public class ContentItemTest {
UriRef mainPart = ci.getPartUri(0);
ci.addPart(mainPart, new Date());
}
+ @Test(expected=IllegalArgumentException.class)
+ public void removeNullPart() {
+ ContentItem ci = new ContentItemImpl(ciUri,blob,new SimpleMGraph()){};
+ ci.removePart(null);
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void removeNegaitveIndexPart() {
+ ContentItem ci = new ContentItemImpl(ciUri,blob,new SimpleMGraph()){};
+ ci.removePart(-1);
+ }
+ @Test(expected=IllegalStateException.class)
+ public void removeMainContentPartByUri() {
+ ContentItem ci = new ContentItemImpl(ciUri,blob,new SimpleMGraph()){};
+ ci.removePart(ci.getPartUri(0));
+ }
+ @Test(expected=IllegalStateException.class)
+ public void removeMainContentPartByIndex() {
+ ContentItem ci = new ContentItemImpl(ciUri,blob,new SimpleMGraph()){};
+ ci.removePart(0);
+ }
+ @Test(expected=NoSuchPartException.class)
+ public void removeNonExistentPartByUri() {
+ ContentItem ci = new ContentItemImpl(ciUri,blob,new SimpleMGraph()){};
+ ci.removePart(new UriRef("urn:does.not.exist:and.can.not.be.removed"));
+ }
+ @Test(expected=NoSuchPartException.class)
+ public void removeNonExistentPartByIndex() {
+ ContentItem ci = new ContentItemImpl(ciUri,blob,new SimpleMGraph()){};
+ ci.removePart(12345);
+ }
+ @Test
+ public void removeRemoveByUri() {
+ ContentItem ci = new ContentItemImpl(ciUri,blob,new SimpleMGraph()){};
+ UriRef uri = new UriRef("urn:content.part:remove.test");
+ ci.addPart(uri, new Date());
+ try {
+ ci.getPart(uri, Date.class);
+ }catch (NoSuchPartException e) {
+ Assert.assertFalse("The part with the uri "+uri+" was not added
correctly",
+ true);
+ }
+ ci.removePart(uri);
+ try {
+ ci.getPart(uri, Date.class);
+ Assert.assertFalse("The part with the uri "+uri+" was not removed
correctly",
+ true);
+ }catch (NoSuchPartException e) {
+ // expected
+ }
+ }
+ @Test
+ public void removeRemoveByIndex() {
+ ContentItem ci = new ContentItemImpl(ciUri,blob,new SimpleMGraph()){};
+ UriRef uri = new UriRef("urn:content.part:remove.test");
+ ci.addPart(uri, new Date());
+ int index = -1;
+ try {
+ for(int i=0; index < 0; i++){
+ UriRef partUri = ci.getPartUri(i);
+ if(partUri.equals(uri)){
+ index = i;
+ }
+ }
+ }catch (NoSuchPartException e) {
+ Assert.assertFalse("The part with the uri "+uri+" was not added
correctly",
+ true);
+ }
+ ci.removePart(index);
+ try {
+ ci.getPart(index, Date.class);
+ Assert.assertTrue("The part with the uri "+uri+" was not removed
correctly",
+ false);
+ }catch (NoSuchPartException e) {
+ // expected
+ }
+ }
+
}