Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Commit

Permalink
PT-1987-projects: Added PrimaryEntityProperty and AbstractPrimaryEnti…
Browse files Browse the repository at this point in the history
…tyProperty.
  • Loading branch information
itaiGershtansky authored and sdumitriu committed Feb 24, 2017
1 parent 0532d7e commit 79d46d3
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
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();

}
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);
}
}
}

0 comments on commit 79d46d3

Please sign in to comment.