public code v1

This commit is contained in:
Francisco Jesús Martínez Mimbrera
2026-05-23 00:32:57 +02:00
commit 759a8968a2
4357 changed files with 163763 additions and 0 deletions
@@ -0,0 +1,91 @@
package flintstones.entity.problemelement.entities;
import java.util.UUID;
import flintstones.entity.extensionenum.ExtensionEnum;
/**
* The Class Alternative. Alternatives can't have parents or children.
*/
public class Alternative extends ProblemElement {
/** The Type. */
public static String Type = Alternative.class.getSimpleName();
/**
* The Enum Fields.
*/
public static enum Fields implements ExtensionEnum {
/** The Alternative. */
Alternative,
/** The Alternatives. */
Alternatives
};
/**
* Instantiates a new alternative.
*/
public Alternative(String name) {
// super();
setName(name);
setType(Alternative.Type);
updateId();
}
/*
* (non-Javadoc)
* º
* @see flintstones.problemelements.ProblemElement#clone()
*/
@Override
public Object clone() {
Alternative result = new Alternative(this.getName());
return result;
}
/*
* (non-Javadoc)
*
* @see flintstones.problemelements.ProblemElement#getChildren()
*/
@Override
public Alternative[] getChildren() {
return new Alternative[0];
}
/*
* (non-Javadoc)
*
* @see flintstones.problemelements.ProblemElement#getParent()
*/
@Override
public Alternative getParent() {
return null;
}
/*
* (non-Javadoc)
*
* @see flintstones.problemelements.ProblemElement#hasChildren()
*/
@Override
public boolean hasChildren() {
return false;
}
/*
* (non-Javadoc)
*
* @see flintstones.problemelements.ProblemElement#hasParent()
*/
@Override
public boolean hasParent() {
return false;
}
@Override
public String calculateId() {
return UUID.randomUUID().toString();
}
}
@@ -0,0 +1,79 @@
package flintstones.entity.problemelement.entities;
import java.util.UUID;
import flintstones.entity.extensionenum.ExtensionEnum;
/**
* The Class Criterion.
*/
public class Criterion extends ProblemElement {
/** The cost. */
private boolean cost;
/** The Type. */
public static String Type = Criterion.class.getSimpleName();
/**
* The Enum Fields.
*/
public static enum Fields implements ExtensionEnum {
/** n elements. */
Criteria,
/** 1 element. */
Criterion,
/** The cost. */
cost
}
/**
* Instantiates a new criterion.
*/
public Criterion(String name) {
setName(name);
this.setType(Criterion.Type);
updateId();
}
/**
* Checks if is cost is cost or benefit.
*
* @return true, if is cost
*/
public boolean isCost() {
return this.cost;
}
/**
* Sets the cost.
*
* @param cost the new cost
*/
public void setCost(boolean cost) {
this.cost = cost;
}
/* (non-Javadoc)
*
* @see flintstones.problemelements.ProblemElement#clone() */
@Override
public Object clone() {
Criterion result = new Criterion(this.getName());
result.cost = cost;
ProblemElement[] children = this.getChildren();
for (ProblemElement item : children)
result.addChildren(item);
if(this.hasParent())
result.setParent(this.parent);
return result;
}
@Override
public String calculateId() {
return UUID.randomUUID().toString();
}
}
@@ -0,0 +1,84 @@
package flintstones.entity.problemelement.entities;
import java.util.UUID;
import flintstones.entity.extensionenum.ExtensionEnum;
/**
* The Class Expert.
*/
public class Expert extends ProblemElement {
/** The Type. */
public static String Type = Expert.class.getSimpleName();
/** The mail. */
private String mail = "";
/**
* The Enum Fields.
*/
public static enum Fields implements ExtensionEnum {
/** The Expert. */
Expert,
/** The Experts. */
Experts,
/** The mail. */
mail
}
/**
* Instantiates a new expert.
*/
public Expert(String name) {
setName(name);
setType(Expert.Type);
updateId();
}
/*
* (non-Javadoc)
*
* @see flintstones.problemelements.ProblemElement#clone()
*/
@Override
public Object clone() {
Expert result = new Expert(this.getName());
result.mail = mail;
ProblemElement[] children = this.getChildren();
for (ProblemElement item : children)
result.addChildren(item);
if (this.hasParent())
result.setParent(this.parent);
return result;
}
/**
* Sets the mail.
*
* @param newMail the new mail
*/
public void setMail(String newMail) {
this.mail = newMail;
}
/**
* Gets the mail.
*
* @return the mail
*/
public String getMail() {
return mail;
}
@Override
public String calculateId() {
return UUID.randomUUID().toString();
}
}
@@ -0,0 +1,74 @@
package flintstones.entity.problemelement.entities;
import org.apache.commons.lang.NotImplementedException;
/**
* The Class FakeProblemElement makes posible to use a dummy ProblemElement with some components.
*/
public class FakeProblemElement extends ProblemElement {
/** The Type. */
public static String Type = FakeProblemElement.class.getSimpleName();
/**
* Instantiates a new fake problem element.
*/
public FakeProblemElement(String name) {
setName(name);
setType(FakeProblemElement.Type);
updateId();
}
/* (non-Javadoc)
* @see flintstones.entity.problemelement.entities.ProblemElement#clone()
*/
@Override
public Object clone() {
throw new NotImplementedException();
}
/**
* As alternative.
*
* @return the problem element
*/
public ProblemElement asAlternative() {
this.setType(Alternative.Type);
return this;
}
/**
* As criterion.
*
* @return the problem element
*/
public ProblemElement asCriterion() {
this.setType(Criterion.Type);
return this;
}
/**
* As expert.
*
* @return the problem element
*/
public ProblemElement asExpert() {
this.setType(Expert.Type);
return this;
}
/**
* As.
*
* @param type the type
*/
public ProblemElement as(String type) {
this.setType(type);
return this;
}
@Override
public String calculateId() {
return getName();
}
}
@@ -0,0 +1,404 @@
package flintstones.entity.problemelement.entities;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import flintstones.entity.common.interfaces.IdentifiedEntity;
import flintstones.entity.extensionenum.ExtensionEnum;
import flintstones.entity.problemelement.exception.DifferentProblemElementTypeException;
/**
* The Class ProblemElement.
*/
public abstract class ProblemElement implements Comparable<ProblemElement>, IdentifiedEntity {
/** The children list. */
protected LinkedList<ProblemElement> children = new LinkedList<>();
/** The item unique name. */
private String name;
/** The parent. */
protected ProblemElement parent = null;
/** The Type. */
public static String Type; // Should be null in parent class
/* IdentifiedEntity implementation */
private String serviceId = null;
private int order = -1;
private int childrenOrder = 0;
/**
* The Enum Fields.
*/
public static enum Fields implements ExtensionEnum {
/** The Problems. */
Problems,
/** The Problem. */
Problem,
/** The id. */
id,
/** The name. */
name,
/** The children. */
children,
/** The Problem element. */
//
ProblemElement
};
protected ProblemElement() {
}
// Children modify methods
/**
* Adds the children.
*
* @param item the item
*/
public void addChildren(ProblemElement item) {
if (item.equals(this))
return;
if (item.hasParent())
item.getParent().removeChildren(item);
item.setParent(this);
item.setOrder(childrenOrder++);
children.add(item);
}
public void addChildrenAfter(ProblemElement previousItem, ProblemElement item) {
ProblemElement[] childrenItems = getChildren();
children.clear();
childrenOrder = 0;
if (previousItem == null) {
addChildren(item);
for (ProblemElement child : childrenItems)
addChildren(child);
} else {
for (ProblemElement child : childrenItems) {
addChildren(child);
if (previousItem.equals(child))
addChildren(item);
}
}
}
public void addChildrenBefore(ProblemElement nextItem, ProblemElement item) {
ProblemElement[] childrenItems = getChildren();
children.clear();
childrenOrder = 0;
for (ProblemElement child : childrenItems) {
if (nextItem.equals(child))
addChildren(item);
addChildren(child);
}
}
/**
* Removes the children.
*
* @param itemUniqueId the item unique id
*/
public void removeChildren(ProblemElement removedChild) {
ProblemElement[] childrenItems = getChildren();
children.clear();
childrenOrder = 0;
for (ProblemElement child : childrenItems) {
if (child.equals(removedChild)) {
child.setOrder(-1);
child.setParent(null);
continue;
}
addChildren(child);
}
//
// for (ProblemElement child : children)
// if (child.equals(removedChild)) {
// children.remove(child);
// return;
// }
}
/**
* Sets the parent.
*
* @param pe the new parent
*/
protected void setParent(ProblemElement pe) {
this.parent = pe;
}
/**
* Change parent.
*
* @param newParent the new parent
* @return true, if successful
*/
public boolean changeParent(ProblemElement newParent) {
ProblemElement item = this;
ProblemElement oldParent = item.getParent();
// Basic check
if (item.getType().equals(Alternative.Type)) {
return false;
}
// throw new AlternativeCantHaveParentException(item);
// Unlink from old parent
if (oldParent != null)
oldParent.removeChildren(item);
// Link with new parent
item.setParent(newParent);
// Add as a child of the item
if (newParent != null) {
// Basic check
if (!item.getType().equals(newParent.getType()))
throw new DifferentProblemElementTypeException(item, newParent);
newParent.addChildren(item);
}
return true;
}
public void setOrder(int pos) {
order = pos;
}
public int getOrder() {
return order;
}
// END Children modify methods
/*
* (non-Javadoc)
*
* @see java.lang.Object#clone()
*/
@Override
public abstract Object clone();
/*
* (non-Javadoc)
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(ProblemElement pe) {
return Comparator.comparing(ProblemElement::getName).thenComparing(ProblemElement::getType).compare(this, pe);
}
/**
* Gets the canonical name. Parent_name > Next_parent > ... > Item_Name
*
* @return the canonical name
*/
public String getCanonicalName() {
if (this.parent != null) {
if(!this.parent.getName().isEmpty()) {
String canonicalTemplate = "{0}>{1}";
return MessageFormat.format(canonicalTemplate, this.parent.getCanonicalName(), this.getName()).toString();
}
}
return this.getName();
}
/**
* Gets shorter item name.
*
* @return the name
*/
public String getShortName() {
return this.name;
}
/**
* Gets the children.
*
* @return the children
*/
public ProblemElement[] getChildren() {
return this.children.toArray(new ProblemElement[this.children.size()]);
}
/**
* Gets the final elements (leaf elements) from this item.
*
* @return the final elements
*/
public ProblemElement[] getFinalElements() {
ArrayList<ProblemElement> items = new ArrayList<>();
// leaf
if (this.children.isEmpty())
items.add(this);
else
for (ProblemElement child : this.children) {
ProblemElement[] arr = child.getFinalElements();
for (ProblemElement pe : arr)
items.add(pe);
}
return items.toArray(new ProblemElement[items.size()]);
}
/**
* Gets the item name.
*
* @return the name
*/
public String getName() {
return this.name;
}
/**
* Gets the parent.
*
* @return the parent
*/
public ProblemElement getParent() {
return this.parent;
}
/**
* Checks for children.
*
* @return true, if successful
*/
public boolean hasChildren() {
return this.children.size() > 0;
}
/**
* Checks for parent.
*
* @return true, if successful
*/
public boolean hasParent() {
return this.parent != null;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
// updateId();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.getName();
}
/**
* Checks if is parent.
*
* @return true, if is parent
*/
public boolean isParent() {
return this.hasChildren();
}
/**
* Checks if is leaf.
*
* @return true, if is leaf
*/
public boolean isLeaf() {
return !isParent();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
HashCodeBuilder hcb = new HashCodeBuilder();
hcb.append(this.getId());
hcb.append(this.getType());
return hcb.toHashCode();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof ProblemElement)) {
return false;
}
ProblemElement other = (ProblemElement) o;
EqualsBuilder builder = new EqualsBuilder();
builder.append(this.getId(), other.getId());
builder.append(this.getType(), other.getType());
return builder.isEquals();
}
@Override
public final String getId() {
return serviceId;
};
@Override
public final void updateId() {
serviceId = calculateId();
}
/* TypedEntity implementation */
private String type;
public String getType() {
return this.type;
}
protected void setType(String type) {
this.type = type;
}
}
@@ -0,0 +1,145 @@
package flintstones.entity.problemelement.entities;
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import flintstones.entity.problemelement.exception.InvalidProblemElementTypeException;
public class ProblemElementHelper {
public static ProblemElement create(String name, String type) {
if(type.equals(Alternative.Type))
return new Alternative(name);
if(type.equals(Expert.Type))
return new Expert(name);
if(type.equals(Criterion.Type))
return new Criterion(name);
if(type.equals(SortingClass.Type))
return new SortingClass(name);
if(type.equals(FakeProblemElement.Type))
return new FakeProblemElement(name);
throw new InvalidProblemElementTypeException(type);
}
public static Alternative[] asAlternatives(ProblemElement[] pes) {
return Arrays.stream(pes).map(k -> (Alternative)k).toArray(Alternative[]::new);
}
public static Criterion[] asCriterions(ProblemElement[] pes) {
return Arrays.stream(pes).map(k -> (Criterion)k).toArray(Criterion[]::new);
}
public static Expert[] asExperts(ProblemElement[] pes) {
return Arrays.stream(pes).map(k -> (Expert)k).toArray(Expert[]::new);
}
public static SortingClass[] asSortingClasses(ProblemElement[] pes) {
return Arrays.stream(pes).map(k -> (SortingClass)k).toArray(SortingClass[]::new);
}
public static String getNextType(String type, boolean next, boolean nextnext) {
int x = 0;
if (Alternative.Type.equals(type))
x = 0;
else if (Expert.Type.equals(type))
x = 1;
else if (Criterion.Type.equals(type))
x = 2;
if (next)
x++;
if (nextnext)
x++;
x = x % 3;
if (x == 0)
return Alternative.Type;
else if (x == 1)
return Expert.Type;
else if (x == 2)
return Criterion.Type;
return null;
}
public static String getImage(ProblemElement pe) {
String type = pe.getType();
boolean isParent = pe.hasChildren();
boolean cost = false;
if(pe.getType().equals(Criterion.Type) && !(pe instanceof FakeProblemElement) ) {
cost = ((Criterion)pe).isCost();
}
return getImage(type, isParent, cost);
}
public static String getImage(String type) {
return getImage(type, false, false);
}
public static String getImage(String type, boolean parent) {
return getImage(type, parent, false);
}
public static String getImage(String type, boolean parent, boolean isCost) {
String f = "problemelement" + File.separator;
if(FakeProblemElement.Type.equals(type))
return f+"fake.png";
if (Alternative.Type.equals(type))
return parent ? f+"alternatives.png" : f+"alternative.png";
if (Expert.Type.equals(type))
return parent ? f+"experts.png": f+"expert.png";
if(SortingClass.Type.equals(type))
return f+"sortclass.png";
if (Criterion.Type.equals(type))
if(parent)
return f+"criterions.png";
else
return isCost ? f+"criterion_cost.png" : f+"criterion.png";
return null;
}
public static ProblemElement[] getAsUserOrdered(ProblemElement[] pes) {
Arrays.sort(pes, getAsUserOrderedComparator());
return pes;
}
public static ProblemElement[] getAsAlphabeticalOrdered(ProblemElement[] pes) {
Arrays.sort(pes, getAlphabeticalComparator());
return pes;
}
// Private
public static Comparator<ProblemElement> getAsUserOrderedComparator(){
Comparator<ProblemElement> c = new Comparator<ProblemElement>() {
public int compare(ProblemElement a, ProblemElement b) {
return a.getOrder() > b.getOrder() ? 1 : -1;
}
};
return c;
}
public static Comparator<ProblemElement> getAlphabeticalComparator(){
Comparator<ProblemElement> c = new Comparator<ProblemElement>() {
public int compare(ProblemElement o1, ProblemElement o2) {
return transformToSInteger(o1.getName()) - transformToSInteger(o2.getName());
}
int transformToSInteger(String str) {
String number = str.replaceAll("\\D", "");
return number.isEmpty() ? 0 : Integer.parseInt(number);
}
};
return c;
}
}
@@ -0,0 +1,80 @@
package flintstones.entity.problemelement.entities;
import java.util.UUID;
import flintstones.entity.extensionenum.ExtensionEnum;
/**
* The Class AHPSortClass.
*/
public class SortingClass extends ProblemElement{
/** The Type. */
public static String Type = SortingClass.class.getSimpleName();
/**
* The Enum Fields.
*/
public static enum Fields implements ExtensionEnum {
/** The name. */
name, AHPSortClass
}
/**
* Instantiates a new AHP sort class.
*
* @param className the class name
*/
public SortingClass(String className) {
setName(className);
setType(SortingClass.Type);
updateId();
}
/* (non-Javadoc)
* @see flintstones.entity.problemelement.entities.ProblemElement#clone()
*/
@Override
public Object clone() {
throw new UnsupportedOperationException();
}
@Override
public String calculateId() {
return UUID.randomUUID().toString();
}
/*
* (non-Javadoc)
*
* @see flintstones.problemelements.ProblemElement#getParent()
*/
@Override
public Alternative getParent() {
return null;
}
/*
* (non-Javadoc)
*
* @see flintstones.problemelements.ProblemElement#hasChildren()
*/
@Override
public boolean hasChildren() {
return false;
}
/*
* (non-Javadoc)
*
* @see flintstones.problemelements.ProblemElement#hasParent()
*/
@Override
public boolean hasParent() {
return false;
}
}
@@ -0,0 +1,163 @@
package flintstones.entity.problemelement.entities;
import java.text.MessageFormat;
import java.util.UUID;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import flintstones.entity.extensionenum.ExtensionEnum;
/**
* The Class AHPSortProfile.
*/
public class SortingProfile extends ProblemElement {
/** The Type. */
public static String Type = SortingProfile.class.getSimpleName();
/**
* The Enum Fields.
*/
public static enum Fields implements ExtensionEnum {
/** The value. */
value,
/** The AHP sort profile. */
SortingProfile,
/** The AHP sort profiles. */
SortingProfiles
};
public static enum SortingProfileType {
Central, Limiting
}
/** The value. */
private double value;
Expert expert;
Criterion criterion;
/**
* Instantiates a new AHP sort profile.
*
* @param limit the limit
* @param position the position
*/
public SortingProfile(Expert e, Criterion c, Double limit) {
// super();
setType(SortingProfile.Type);
expert = e;
criterion = c;
setValue(limit);
setName("P_" + c.getName() + "_" + e.getName() + " " + value + "");
updateId();
}
/*
* (non-Javadoc)
*
* @see flintstones.entity.problemelement.entities.ProblemElement#clone()
*/
@Override
public Object clone() {
throw new UnsupportedOperationException();
}
/**
* Gets the value.
*
* @return the value
*/
public double getValue() {
return value;
}
/**
* Sets the value.
*
* @param val the new value
*/
public void setValue(double val) {
this.value = val;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
HashCodeBuilder hcb = new HashCodeBuilder();
hcb.append(this.getName());
hcb.append(this.getType());
hcb.append(this.getValue());
hcb.append(this.expert.getName());
hcb.append(this.criterion.getName());
return hcb.toHashCode();
}
/*
* (non-Javadoc)
*
* @see
* flintstones.entity.problemelement.entities.ProblemElement#equals(java.lang.
* Object)
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof SortingProfile)) {
return false;
}
SortingProfile other = (SortingProfile) o;
EqualsBuilder builder = new EqualsBuilder();
builder.append(this.getName(), other.getName());
builder.append(this.getType(), other.getType());
builder.append(this.getValue(), other.getValue());
builder.append(this.expert.getName(), other.expert.getName());
builder.append(this.criterion.getName(), other.criterion.getName());
return builder.isEquals();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String template = "{1}";
// String template = "[{0}] {1} -> {2}";
String x = MessageFormat.format(template, Type, getName(), this.value).toString();
return x;
}
@Override
public String calculateId() {
return UUID.randomUUID().toString();
}
public ProblemElement getExpert() {
return expert;
}
public ProblemElement getCriterion() {
return criterion;
}
@Override
public String getShortName() {
return "P " + value;
}
}