absolute bruh moment

This commit is contained in:
2024-02-02 15:12:56 -06:00
parent ad38d64219
commit 31f634c637
11 changed files with 615 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>MP1_ManyToMany_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.mp1_manytomany_calebfontenot.MP1_ManyToMany_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,114 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.mp1_manytomany_calebfontenot;
/**
*
* @author caleb
*/
import java.util.List;
import java.util.Set;
/**
*
* @author A. V. Markou
* @param <Many1>
* @param <Many2>
*/
public interface ManyToMany<Many1, Many2>
{
/**
* 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.
* @return the previous value associated with Many1, or null if there was no
* Many1 with the same "key".
* @throws ClassCastException - if the class of the specified Many1, or
* Many2 or value prevents it from being stored in this map( i,e Many1 is
* String and you pass an object)
* @throws NullPointerException - if the specified Many1 or Many2 is null
* and ManyToMany does not permit nulls as values.
* @throw IllegalArgumentException - if a duplicate exists in childrenRight
* list ( s1 --> p1,p2,p2 is not allowed).
* @return the previous value associated with parentLeft, or null if there
* was no childrenRight for parentLeft.
*/
List<Many2> add(Many1 parentLeft, Many2... childrenRight);
/**
* 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.
*/
List<Many1> getChildrenLeft(Many2 parentRight);
/**
* 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.
*/
List<Many2> getChildrenRight(Many1 parentLeft);
/**
* Returns a set of the Many1 elements that exist on the LHS of the many to
* many relationship.
*
* @return Set of Many1
*/
Set<Many1> getParentsLeft();
/**
* Returns a set of the Many2 elements that exist on the RHS of the many to
* many relationship.
*
* @return Set of Many2
*/
Set<Many2> getParentsRight();
/**
* Removes the many1 parameter from the LHS of the many relationship AND all
* its corresponding values that exist in the RHS of the many to many
* relationship. For example given: ( LHS e1: p1, p2 e2: p2, p3 RHS: p1: e1
* p2: e1, e2 p3: e2 after removing e1 from the LHS will results into: ( LHS
* e2: p2, p3 RHS: p2: e2 p3: e2
*
* @param many1 the unique element on the LHS to be removed.
* @throws NullPointerException if parameter many1 is null.
* @return true if the removal occurred, false if many1 does not exist in
* the LHS of the many to many relationship.
*/
boolean removeLeft(Many1 many1);
/**
* Removes the many1 parameter from the RHS of the many relationship AND all
* its corresponding values that exist in the LHS of the many to many
* relationship. For example given: LHS e2: p2, p3 RHS: p2: e2 p3: e2 after
* removing p2 from the RHS will results into: LHS e2: p3 RHS p3: e2
*
*
* @param many2 the unique element on the LHS to be removed.
* @throws NullPointerException if parameter many1 is null.
* @return true if the removal occurred, false if many1 does not exist in
* the LHS of the many to many relationship.
*/
boolean removeRight(Many2 many2);
/**
* Clears all.
*
*/
void clear();
}

View File

@@ -0,0 +1,182 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.mp1_manytomany_calebfontenot;
/**
*
* @author caleb
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
* @author asdv5
*/
public class ManyToManyFactory {
public static <Many1, Many2> //generic types to be used in the method
ManyToMany< Many1, Many2>//return type
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();
@Override
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.
*
* @param parentLeft - exactly one Many1 object.
* @param childrenRight - one or more Many2 objects.
* @return the previous value associated with Many1, or null if there was no Many1 with the same "key".
* @throws ClassCastException - if the class of the specified Many1, or Many2 or value prevents it from being stored in this map( i,e Many1 is String and you pass an object)
* @throws NullPointerException - if the specified Many1 or Many2 is null and ManyToMany does not permit nulls as values.
* @throw IllegalArgumentException - if a duplicate exists in childrenRight list ( s1 --> p1,p2,p2 is not allowed).
* @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>();
// Check to see if values already exist in this many to many object
if (this.left != parentLeft && this.left != null) {
returnList.add((Many2) this.left);
this.left = (Map<Object, Object>) parentLeft;
}
if (this.left != childrenRight && this.right != null) {
returnList.add((Many2) this.right);
this.left = (Map<Object, Object>) childrenRight;
left.put(parentLeft, Arrays.asList(childrenRight));
}
}
@Override
public List<Many1> getChildrenLeft(Many2 parentRight)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public List<Many2> getChildrenRight(Many1 parentLeft)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public Set<Many1> getParentsLeft()
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public Set<Many2> getParentsRight()
{
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()
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
};
}
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("getChildrenLeft(p1) returns: " + mm.getChildrenLeft("p2"));
System.out.println("getChildrenLeft(p2) returns: " + mm.getChildrenLeft("p3"));
System.out.println("getChildrenRight(e1) returns: " + mm.getChildrenRight("e1"));
System.out.println("getChildrenRight(e1) returns: " + mm.getChildrenRight("e2"));
System.out.println("-----------------------------------------------");
System.out.println("The internal hash maps of ManyToMany after insertions: " + mm);
System.out.println("----------AFTER REMOVAL of e1 LEFT----------------------------------------");
System.out.println("removeLeft(e1) returns: " + mm.removeLeft("e1"));
System.out.println("getParentsLeft returns: " + mm.getParentsLeft());
System.out.println("getParentsRight returns: " + mm.getParentsRight());
System.out.println("getChildrenRight(e2) returns: " + mm.getChildrenRight("e2"));
System.out.println("getChildrenLeft(p2) returns: " + mm.getChildrenLeft("p2"));
System.out.println("getChildrenLeft(p3) returns: " + mm.getChildrenLeft("p3"));
System.out.println("The internal hash maps of ManyToMany after removal LEFT: " + mm);
System.out.println("\n----------AFTER REMOVAL of p2 RIGHT----------------------------------------");
System.out.println("removeLeft(p2) returns: " + mm.removeRight("p2"));
System.out.println("getParentsLeft returns: " + mm.getParentsLeft());
System.out.println("getParentsRight returns: " + mm.getParentsRight());
System.out.println("getChildrenRight(e2) returns: " + mm.getChildrenRight("e2"));
System.out.println("getChildrenLeft(p3) returns: " + mm.getChildrenLeft("p3"));
System.out.println("--------------------------------------------------");
System.out.println("mm.removeLeft(e1) returns: " + mm.removeLeft("e1"));
System.out.println("The internal hash maps of ManyToMany after removal RIGHT: " + mm);
Thread.sleep(5000, 0);
System.out.println("---------------CATCHING EXCEPTIONS -----------------------------------");
try {
mm.getChildrenRight("e10");//e10 dos not exist
} catch (RuntimeException e) {
System.err.println("getChildrenRight for e10 throws exception:" + e);
}
try {
System.out.println(mm.add("e6", new String[]{
"value1", null
}));
} catch (RuntimeException e) {
System.err.println("add(e6, new String[]{null } ) throws exception: " + e);
}
try {
System.out.println(mm.add(null, "p1"));
} catch (RuntimeException e) {
System.err.println("add(null, p1)returns throws exception: " + e);
}
try {
System.out.println(mm.getChildrenLeft("p1"));
} catch (RuntimeException e) {
System.err.println("getChildrenRight(p1) throws exception: " + e);
}
try {
mm.add((String) new Object(), (String) new Object());
} catch (RuntimeException e) {
System.err.println("add((String) new Object(), (String) new Object()) throws exception: " + e);
}
mm.clear();
System.out.println("-----------------------------------------------");
System.out.println("The internal hash maps of ManyToMany after clear: " + mm);
}
}