This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PT-1987-projects: Added PrimaryEntityProperty and AbstractPrimaryEnti…
…tyProperty.
- Loading branch information
1 parent
0532d7e
commit 79d46d3
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
components/entities/api/src/main/java/org/phenotips/entities/PrimaryEntityProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/ | ||
*/ | ||
package org.phenotips.entities; | ||
|
||
import org.xwiki.stability.Unstable; | ||
|
||
/** | ||
* A property of a {@link PrimaryEntity}, which in turn is another type of entity. For example, a Project has an image, | ||
* a Patient has a template. | ||
* | ||
* @param <E> the type of the property | ||
* @version $Id$ | ||
* @since 1.3M2 | ||
*/ | ||
@Unstable("New API introduced in 1.3") | ||
public interface PrimaryEntityProperty<E extends PrimaryEntity> extends PrimaryEntity | ||
{ | ||
/** | ||
* Returns the property, if exists. | ||
* | ||
* @return a property, or null | ||
*/ | ||
E get(); | ||
|
||
/** | ||
* Sets the property. | ||
* | ||
* @param property the property | ||
* @return {@code true} if the property was successfully set, or was already set, {@code false} if the | ||
* operation failed | ||
*/ | ||
boolean set(E property); | ||
|
||
/** | ||
* Removes the property. | ||
* | ||
* @return {@code true} if the property was successfully removed, or if it wasn't set, {@code false} if the | ||
* operation failed | ||
*/ | ||
boolean remove(); | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...ties/api/src/main/java/org/phenotips/entities/internal/AbstractPrimaryEntityProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/ | ||
*/ | ||
package org.phenotips.entities.internal; | ||
|
||
import org.phenotips.entities.PrimaryEntity; | ||
import org.phenotips.entities.PrimaryEntityProperty; | ||
|
||
import org.xwiki.model.reference.DocumentReference; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
import com.xpn.xwiki.doc.XWikiDocument; | ||
|
||
/** | ||
* Base class for implementing PrimaryEntityProperty. The implementation is done as a container of one object. | ||
* | ||
* @param <E> the type of entity of the property | ||
* @version $Id$ | ||
* @since 1.3M2 | ||
*/ | ||
public abstract class AbstractPrimaryEntityProperty<E extends PrimaryEntity> | ||
extends AbstractContainerPrimaryEntityGroup<E> | ||
implements PrimaryEntityProperty<E> | ||
{ | ||
protected AbstractPrimaryEntityProperty(XWikiDocument document) | ||
{ | ||
super(document); | ||
} | ||
|
||
protected AbstractPrimaryEntityProperty(DocumentReference reference) | ||
{ | ||
super(reference); | ||
} | ||
|
||
@Override | ||
public E get() | ||
{ | ||
Collection<E> members = this.getMembers(); | ||
Iterator<E> iterator = members.iterator(); | ||
if (iterator.hasNext()) { | ||
return iterator.next(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean set(E property) | ||
{ | ||
if (property == null) { | ||
return false; | ||
} | ||
if (!this.remove()) { | ||
return false; | ||
} | ||
return addMember(property); | ||
} | ||
|
||
@Override | ||
public boolean remove() | ||
{ | ||
E property = this.get(); | ||
if (property == null) { | ||
return true; | ||
} else { | ||
return this.removeMember(property); | ||
} | ||
} | ||
} |