broken code :)

This commit is contained in:
2024-02-08 14:01:43 -06:00
parent a48615c8f8
commit eb94a026a7
11 changed files with 4582 additions and 43 deletions

View File

@@ -14,6 +14,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
@@ -24,24 +25,20 @@ public class ManyToManyFactory {
public static <Many1, Many2> //generic types to be used in the method
ManyToMany< Many1, Many2>//return type
createManyToMany()
{
createManyToMany() {
return new ManyToMany<Many1, Many2>() {
//private HashSet parents = new HashSet();
private Map<Object, Object> left = new HashMap();
private Map<Object, Object> right = new HashMap();
private HashSet<Many1> parents = new HashSet<>();
private Map<Object, Object> left = new HashMap<>();
private Map<Object, Object> right = new HashMap<>();
@Override
public String toString()
{
public String toString() {
return "{" + "left=" + left + ", right=" + right + '}';
}
/**
* Creates a Many to Many relationship between the parentLeft and the childrenRight.
* Example for ( Many1 a, Many2 1, 2, 3) it creates --> (a 1) ( a 2 ) ( a 3 ) and ( 1, a ), ( 2, a ), ( 3, a).
* No duplicate values of Many2 are allowed.
* Creates a Many to Many relationship between the parentLeft and the childrenRight. Example for ( Many1 a, Many2 1, 2, 3) it creates --> (a 1) ( a 2 ) ( a 3 ) and ( 1, a ), ( 2, a ), ( 3, a). No duplicate values of Many2 are allowed.
*
* @param parentLeft - exactly one Many1 object.
* @param childrenRight - one or more Many2 objects.
@@ -52,70 +49,126 @@ public class ManyToManyFactory {
* @return the previous value associated with parentLeft, or null if there was no childrenRight for parentLeft.
*/
@Override
public List<Many2> add(Many1 parentLeft, Many2... childrenRight)
{
List<Many2> returnList = new ArrayList<Many2>();
public List<Many2> add(Many1 parentLeft, Many2... childrenRight) {
HashSet<Many2> verifyUnique = new HashSet<>();
List<Many2> returnList = new ArrayList<>();
// Check for exceptions
if (!childrenRight.equals(parentLeft.getClass())) {
throw new ClassCastException();
//throw new ClassCastException();
}
if (parentLeft == null || childrenRight == null) {
throw new NullPointerException();
}
// Try to add all elements from childrenRight into HashSet. If we're unable to, through an IllegalArgumentException, assuming HashSet doesn't throw one already.
verifyUnique.addAll(Arrays.asList(childrenRight));
if (left.size() > 0 && childrenRight.length > 0) {
for (Object e : left.values()) {
returnList.add((Many2) e);
}
//returnList.addAll((<? extends Many2>) left.values());
}
// Keep track of the parents so we can manipulate the RHS
parents.add(parentLeft);
// Handle LHS
//for (Many2 e : childrenRight) {
left.put(parentLeft, new ArrayList<Many2>(Arrays.asList(childrenRight)));
//}
//Handle RHS
/*
for (Many1 e: parents) {
right.put(, e );
}
*/
if (returnList.size() == 0) {
return null;
}
left.put(parentLeft, new ArrayList<Many2>(Arrays.asList(childrenRight)));
return returnList;
}
/**
* Returns the List of all left children of the parentRight.
*
* @param parentRight a parent at the RHS of the many to many relationship.
* @return the List of all left children of the parentRight.
* @throw IllegalArgumentException if the value of parameter parentRight does not exist in the RHS of the many to many relationship.
*/
@Override
public List<Many1> getChildrenLeft(Many2 parentRight)
{
public List<Many1> getChildrenLeft(Many2 parentRight) {
List<Many1> returnList = new ArrayList<Many1>();
for (Entry<Object, Object> ee : left.entrySet()) {
System.out.println(ee.getKey() + ", " + parentRight);
if (ee.getKey().equals(parentRight)) {
returnList.add((Many1) ee.getValue());
}
}
if (returnList.size() > 0) {
return null;
}
return returnList;
}
/**
* Returns the List of all right children of the parentLeft.
*
* @param parentLeft a parent at the LHS of the many to many relationship.
* @return the List of all right children of the parentLeft.
* @throws IllegalArgumentException if the value of parameter parentLeft does not exist on the LHS of the many to many relationship.
*/
@Override
public List<Many2> getChildrenRight(Many1 parentLeft) {
List<Many2> returnList = new ArrayList<>();
for (Entry<Object, Object> ee : right.entrySet()) {
if (ee.getKey() == parentLeft) {
returnList.add((Many2) ee.getValue());
}
}
if (returnList.size() > 0) {
return null;
}
return returnList;
}
/**
* Returns a set of the Many1 elements that exist on the LHS of the many to many relationship.
*
* @return Set of Many1
*/
@Override
public Set<Many1> getParentsLeft() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public List<Many2> getChildrenRight(Many1 parentLeft)
{
public Set<Many2> getParentsRight() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public Set<Many1> getParentsLeft()
{
public boolean removeLeft(Many1 many1) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public Set<Many2> getParentsRight()
{
public boolean removeRight(Many2 many2) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public boolean removeLeft(Many1 many1)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public boolean removeRight(Many2 many2)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void clear()
{
public void clear() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
};
}
public static void main(String[] args) throws InterruptedException
{
public static void main(String[] args) throws InterruptedException {
ManyToMany<String, String> mm = ManyToManyFactory.createManyToMany();
//mm.add(1, 1);///will not compile, we have Many1, Many2 as String
System.out.println("add(e1, p1, p2)returns: " + mm.add("e1", "p1", "p2"));
System.out.println("add(e2, p2, p3)returns: " + mm.add("e2", "p2", "p3"));
System.out.println("getParentsLeft returns: " + mm.getParentsLeft());
System.out.println("getParentsRight returns: " + mm.getParentsRight());
//System.out.println("getParentsLeft returns: " + mm.getParentsLeft());
//System.out.println("getParentsRight returns: " + mm.getParentsRight());
System.out.println("getChildrenLeft(p1) returns: " + mm.getChildrenLeft("p2"));
System.out.println("getChildrenLeft(p2) returns: " + mm.getChildrenLeft("p3"));
System.out.println("getChildrenRight(e1) returns: " + mm.getChildrenRight("e1"));