Compare commits

...

17 Commits

149 changed files with 16962 additions and 418 deletions

15
.gitignore vendored
View File

@@ -194,3 +194,18 @@
/Semester 3/Assignments/LinkedList/build/ /Semester 3/Assignments/LinkedList/build/
/Semester 3/Assignments/MP6_CalebFontenot/target/ /Semester 3/Assignments/MP6_CalebFontenot/target/
/Semester 3/Assignments/Lab_CalebFontenot_MaximumOrderedString/target/ /Semester 3/Assignments/Lab_CalebFontenot_MaximumOrderedString/target/
/Semester 4/EnumDemo/target/
/Semester 4/Assignments/DAO/target/
/Semester 4/Assignments/ProjectTrees_CalebFontenot/target/
/Semester 4/Assignments/DataStructures/target/
/MergeSort/target/
/Semester 3/Exams/ProgrammingExam2_CalebFontenot/target/
/Semester 4/Assignments/HashMapASDV_Ant/nbproject/private/
/Semester 4/Assignments/HashMapASDV_Ant/build/
/Semester 4/Assignments/HashMapASDV_Ant/dist/
/Semester 4/Assignments/MP1_ManyToMany_CalebFontenot/target/
/Semester 4/Assignments/Multithreading_CalebFontenot/target/
/Semester 4/Assignments/MapASDV_CalebFontenot/nbproject/private/
/Semester 4/Assignments/MapASDV_CalebFontenot/build/
/Semester 4/Assignments/Hashing_CalebFontenot/target/
/Semester 4/Assignments/MP2_CalebFontenot/target/

14
MergeSort/pom.xml Normal file
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>com.calebfontenot</groupId>
<artifactId>MergeSort</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>com.calebfontenot.mergesort.MergeSort</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,88 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.calebfontenot.mergesort;
/**
*
* @author caleb
*/
public class MergeSort {
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
public static void main(String args[]) {
// merge sort = recursively divide array in 2, sort, re-combine
// run-time complexity = O(n Log n)
// space complexity = O(n)
int[] array = {8, 2, 5, 3, 4, 7, 6, 1, 0};
printArray(array);
mergeSort(array);
printArray(array);
}
private static void mergeSort(int[] array) {
int length = array.length;
if (length <= 1) {
return; //base case
}
int middle = length / 2;
int[] leftArray = new int[middle];
int[] rightArray = new int[length - middle];
int i = 0; //left array
int j = 0; //right array
for (; i < length; i++) {
if (i < middle) {
leftArray[i] = array[i];
} else {
rightArray[j] = array[i];
j++;
}
}
mergeSort(leftArray);
mergeSort(rightArray);
merge(leftArray, rightArray, array);
}
private static void merge(int[] leftArray, int[] rightArray, int[] array) {
int leftSize = array.length / 2;
int rightSize = array.length - leftSize;
int i = 0, l = 0, r = 0; //indices
//check the conditions for merging
while (l < leftSize && r < rightSize) {
if (leftArray[l] < rightArray[r]) {
array[i] = leftArray[l];
i++;
l++;
} else {
array[i] = rightArray[r];
i++;
r++;
}
}
while (l < leftSize) {
array[i] = leftArray[l];
i++;
l++;
}
while (r < rightSize) {
array[i] = rightArray[r];
i++;
r++;
}
}
}

View File

@@ -0,0 +1,55 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>CompareSize.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.number {color: #6897bb}
.ST1 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/Lab_CalebFontenot_MaximumOrderedString/src/main/java/edu/slcc/asdv/caleb/lab_calebfontenot_maximumorderedstring/CompareSize.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> edu.slcc.asdv.caleb.lab_calebfontenot_maximumorderedstring;
<span class="literal">import</span> java.util.Comparator;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> CompareSize <span class="literal">implements</span> Comparator {
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST1">compare</span>(Object o1, Object o2)
{
<span class="literal">int</span> str1 = ((String) o1).length();
<span class="literal">int</span> str2 = ((String) o2).length();
<span class="literal">if</span> (str1 &gt; str2) {
<span class="literal">return</span> -<span class="number">1</span>;
} <span class="literal">else</span> <span class="literal">if</span>(str1 == str2) {
<span class="literal">return</span> <span class="number">0</span>;
} <span class="literal">else</span> <span class="literal">if</span> (str1 &lt; str2) {
<span class="literal">return</span> <span class="number">1</span>;
}
<span class="literal">return</span> Integer.<span class="ST2">MAX_VALUE</span>;
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,74 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>FindMax.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.number {color: #6897bb}
.string {color: #6a8759}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/Lab_CalebFontenot_MaximumOrderedString/src/main/java/edu/slcc/asdv/caleb/lab_calebfontenot_maximumorderedstring/FindMax.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> edu.slcc.asdv.caleb.lab_calebfontenot_maximumorderedstring;
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.Collections;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">Comparator</span><span class="comment">;</span>
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">Scanner</span><span class="comment">;</span>
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> FindMax {
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST1">findMax</span>(String input) {
<span class="literal">int</span> iterationCount = <span class="number">0</span>;
ArrayList&lt;String&gt; possibleSubStr = <span class="literal">new</span> ArrayList&lt;&gt;();
String currentStr = <span class="string">&quot;&quot;</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; input.length() -<span class="number">2</span>; ++i) {
currentStr = input.charAt(i) + <span class="string">&quot;&quot;</span>;
<span class="literal">for</span>(<span class="literal">int</span> j = i + <span class="number">1</span>; j &lt; input.length() -<span class="number">1</span>; ++j) {
iterationCount++;
<span class="literal">if</span> (input.toLowerCase().charAt(i) &lt; input.toLowerCase().charAt(j)) {
currentStr += input.charAt(j);
} <span class="literal">else</span> {
possibleSubStr.add(currentStr);
<span class="literal">break</span>;
}
}
}
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Iteration count: </span><span class="string">&quot;</span> + iterationCount);
System.<span class="ST2">out</span>.println(possibleSubStr);
Collections.<span class="ST3">sort</span>(possibleSubStr, <span class="literal">new</span> CompareSize());
<span class="literal">return</span> possibleSubStr.get(<span class="number">0</span>);
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args)
{
<span class="comment">//System.out.print(&quot;Enter a string: &quot;);</span>
<span class="comment">//Scanner input = new Scanner(System.in);</span>
String inputString = <span class="string">&quot;</span><span class="string">abcdabcdefgzabcdefadcdefab</span><span class="string">&quot;</span>;
<span class="comment">//input.nextLine();</span>
System.<span class="ST2">out</span>.print(<span class="string">&quot;</span><span class="string">The maximum sorted subString is: </span><span class="string">&quot;</span>);
System.<span class="ST2">out</span>.println(<span class="ST3">f</span><span class="ST3">indMax</span>(inputString));
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,62 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>FindMaxOrderedSubstring.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/Lab_CalebFontenot_MaximumOrderedString/src/main/java/edu/slcc/asdv/caleb/lab_calebfontenot_maximumorderedstring/FindMaxOrderedSubstring.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> edu.slcc.asdv.caleb.lab_calebfontenot_maximumorderedstring;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">ArrayList</span><span class="comment">;</span>
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">Collections</span><span class="comment">;</span>
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> FindMaxOrderedSubstring {
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST1">findMaxOrderedSubString</span>(String input) {
String outputString = <span class="string">&quot;&quot;</span>;
<span class="literal">int</span> j = <span class="number">0</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; input.length(); ++i) {
<span class="literal">if</span> (i == <span class="number">0</span>) {
outputString += input.charAt(i);
}
<span class="literal">if</span> (input.charAt(i) &gt; outputString.charAt(j)) {
++j;
outputString += input.charAt(i);
}
}
<span class="literal">return</span> outputString;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
System.<span class="ST2">out</span>.println(<span class="ST3">f</span><span class="ST3">indMaxOrderedSubString</span>(<span class="string">&quot;</span><span class="string">Welcome!</span><span class="string">&quot;</span>));
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,30 @@
/*
* 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.lab_calebfontenot_maximumorderedstring;
import java.util.Comparator;
/**
*
* @author caleb
*/
public class CompareSize implements Comparator {
@Override
public int compare(Object o1, Object o2)
{
int str1 = ((String) o1).length();
int str2 = ((String) o2).length();
if (str1 > str2) {
return -1;
} else if(str1 == str2) {
return 0;
} else if (str1 < str2) {
return 1;
}
return Integer.MAX_VALUE;
}
}

View File

@@ -15,11 +15,13 @@ import java.util.Scanner;
*/ */
public class FindMax { public class FindMax {
public static String findMax(String input) { public static String findMax(String input) {
int iterationCount = 0;
ArrayList<String> possibleSubStr = new ArrayList<>(); ArrayList<String> possibleSubStr = new ArrayList<>();
String currentStr = ""; String currentStr = "";
for (int i = 0; i < input.length() -2; ++i) { for (int i = 0; i < input.length() -2; ++i) {
currentStr = input.charAt(i) + ""; currentStr = input.charAt(i) + "";
for(int j = i + 1; j < input.length() -1; ++j) { for(int j = i + 1; j < input.length() -1; ++j) {
iterationCount++;
if (input.toLowerCase().charAt(i) < input.toLowerCase().charAt(j)) { if (input.toLowerCase().charAt(i) < input.toLowerCase().charAt(j)) {
currentStr += input.charAt(j); currentStr += input.charAt(j);
} else { } else {
@@ -28,8 +30,9 @@ public class FindMax {
} }
} }
} }
System.out.println("Iteration count: " + iterationCount);
System.out.println(possibleSubStr);
Collections.sort(possibleSubStr, new CompareSize()); Collections.sort(possibleSubStr, new CompareSize());
//System.out.println(possibleSubStr);
return possibleSubStr.get(0); return possibleSubStr.get(0);
} }
public static void main(String[] args) public static void main(String[] args)
@@ -41,22 +44,4 @@ public class FindMax {
System.out.print("The maximum sorted subString is: "); System.out.print("The maximum sorted subString is: ");
System.out.println(findMax(inputString)); System.out.println(findMax(inputString));
} }
}
class CompareSize implements Comparator {
@Override
public int compare(Object o1, Object o2)
{
int str1 = ((String) o1).length();
int str2 = ((String) o2).length();
if (str1 > str2) {
return -1;
} else if(str1 == str2) {
return 0;
} else if (str1 < str2) {
return 1;
}
return Integer.MAX_VALUE;
}
} }

View File

@@ -0,0 +1,38 @@
/*
* 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.lab_calebfontenot_maximumorderedstring;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
*
* @author caleb
*/
public class FindMaxOrderedSubstring {
public static String findMaxOrderedSubString(String input) {
String outputString = "";
int j = 0;
for (int i = 0; i < input.length(); ++i) {
if (i == 0) {
outputString += input.charAt(i);
}
if (input.charAt(i) > outputString.charAt(j)) {
System.out.println(++j);
outputString += input.charAt(i);
}
}
return outputString;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputStr = input.nextLine();
System.out.println(findMaxOrderedSubString(inputStr));
}
}

View File

@@ -0,0 +1,105 @@
/*
* 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.lab_calebfontenot_maximumorderedstring;
/**
*
* @author caleb
*/
import java.util.Comparator;
public class GenericMergeSort {
public static <E extends Comparable<E>> void mergeSort(E[] list)
{
mergeSort(list,
new Comparator<E>() {
@Override
public int compare(E e1, E e2)
{
return ((Comparable<E>) e1).compareTo(e2);
}
});
}
static int recursiveCallCount = 0;
public static <E> void mergeSort(E[] list,
Comparator<? super E> comparator)
{
if (list.length > 1) {
recursiveCallCount++;
// Merge sort the first half
E[] firstHalf = (E[]) new Object[list.length / 2];
System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
mergeSort(firstHalf, comparator);
// Merge sort the second half
int secondHalfLength = list.length - list.length / 2;
E[] secondHalf = (E[]) new Object[secondHalfLength];
System.arraycopy(list, list.length / 2,
secondHalf, 0, secondHalfLength);
mergeSort(secondHalf, comparator);
// Merge firstHalf with secondHalf
E[] temp = merge1(firstHalf, secondHalf, comparator);
System.arraycopy(temp, 0, list, 0, temp.length);
}
}
private static <E> E[]
merge1(E[] list1, E[] list2, Comparator<? super E> comparator)
{
E[] temp = (E[]) new Object[list1.length + list2.length];
int current1 = 0; // Index in list1
int current2 = 0; // Index in list2
int current3 = 0; // Index in temp
while (current1 < list1.length && current2 < list2.length) {
if (comparator.compare(list1[current1], list2[current2]) < 0) {
temp[current3++] = list1[current1++];
} else {
temp[current3++] = list2[current2++];
}
}
while (current1 < list1.length) {
temp[current3++] = list1[current1++];
}
while (current2 < list2.length) {
temp[current3++] = list2[current2++];
}
return temp;
}
public static void main(String[] args)
{
Integer[] list
= {
2, 3, 2, 5, 6, 1, -2, 3, 14, 12
};
mergeSort(list);
System.out.println("number of recursive calls: " + recursiveCallCount);
recursiveCallCount = 0;
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
System.out.println();
String[] list1
= {
"ABC", "abc", "abm", "Anf", "Good", "Bad", "nice"
};
mergeSort(list1, (s1, s2) -> s1.compareToIgnoreCase(s2));
System.out.println("number of recursive calls: " + recursiveCallCount);
recursiveCallCount = 0;
for (int i = 0; i < list1.length; i++) {
System.out.print(list1[i] + " ");
}
}
}

View File

@@ -0,0 +1,56 @@
/*
* 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.lab_calebfontenot_maximumorderedstring;
/**
*
* @author caleb
*/
public class JudeFindMax {
public static void main(String[] args) {
System.out.println(consecLetters("abcabcdgabxy"));
System.out.println(consecLetters("abcabcdgabmnsxy"));
System.out.println(consecLetters("abchjsfhajshfhijklmnopqjfaksfhkajhsf"));
System.out.println(consecLetters("hnafffgardghikortmmnmnmn"));
}
public static String consecLetters(String input)
{
String consec = "";
String maxConsec = "";
for (int i = 1; i < input.length(); i++) //n - 1
{
//If two in order
if (input.charAt(i) > input.charAt(i - 1))
{
//If length is zero then add previous as well
if (consec.length() == 0)
consec += input.charAt(i - 1);
consec += input.charAt(i);
}
//If not in order
else
{
//Check current consec length against maximum length
if (consec.length() > maxConsec.length())
{
//set new max
maxConsec = consec;
}
consec = "";
}
}
//Check current consec length against maximum length
if (consec.length() > maxConsec.length())
{
//set new max
maxConsec = consec;
}
return "Most letters found in order: " + maxConsec;
}
}

View File

@@ -0,0 +1,2 @@
#!/bin/bash
java --module-path /usr/lib --add-modules javafx.fxml,javafx.controls,javafx.media -XX:+UnlockExperimentalVMOptions -XX:+EagerJVMCI -XX:+EnableJVMCI -XX:+UseJVMCICompiler -XX:+UseJVMCINativeLibrary -Djava.awt.graphicsenv=net.java.openjdk.cacio.wayland.WaylandGraphicsEnvironment -jar dist/MP2-chapter4_Java20_CalebFontenot.jar

View File

@@ -1 +0,0 @@
,caleb,caleb-gaming-laptop-archlinux,20.10.2023 21:26,file:///home/caleb/.config/libreoffice/4;

View File

@@ -0,0 +1,286 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>AddressBookFX.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST2 {color: #ffc66d}
.ST5 {color: #9876aa}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST4 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/AddressBookFX.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="literal">import</span> java.io.ObjectInputStream;
<span class="literal">import</span> java.io.ObjectOutputStream;
<span class="literal">import</span> java.io.Serializable;
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> javafx.application.Application;
<span class="comment">import</span> <span class="comment">javafx</span><span class="comment">.</span><span class="comment">geometry</span><span class="comment">.</span><span class="comment">HPos</span><span class="comment">;</span>
<span class="literal">import</span> javafx.geometry.Pos;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.control.Button;
<span class="literal">import</span> javafx.scene.control.Label;
<span class="literal">import</span> javafx.scene.control.TextField;
<span class="literal">import</span> javafx.scene.layout.ColumnConstraints;
<span class="literal">import</span> javafx.scene.layout.GridPane;
<span class="literal">import</span> javafx.scene.layout.BorderPane;
<span class="comment">import</span> <span class="comment">javafx</span><span class="comment">.</span><span class="comment">scene</span><span class="comment">.</span><span class="comment">layout</span><span class="comment">.</span><span class="comment">RowConstraints</span><span class="comment">;</span>
<span class="literal">import</span> javafx.stage.Stage;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> AddressBookFX <span class="literal">extends</span> Application {
<span class="literal">static</span> File <span class="ST1">addressBook</span> = <span class="literal">new</span> File(<span class="string">&quot;</span><span class="string">AddressBookFX.dat</span><span class="string">&quot;</span>);
<span class="literal">static</span> <span class="literal">int</span> <span class="ST1">addressArrayPointer</span> = <span class="number">0</span>;
<span class="literal">static</span> ArrayList&lt;AddressBookEntry&gt; <span class="ST1">addressArray</span> = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">static</span> TextField <span class="ST1">nameTextField</span> = <span class="literal">new</span> TextField();
<span class="literal">static</span> TextField <span class="ST1">streetTextField</span> = <span class="literal">new</span> TextField();
<span class="literal">static</span> TextField <span class="ST1">cityTextField</span> = <span class="literal">new</span> TextField();
<span class="literal">static</span> TextField <span class="ST1">stateTextField</span> = <span class="literal">new</span> TextField();
<span class="literal">static</span> TextField <span class="ST1">zipTextField</span> = <span class="literal">new</span> TextField();
<span class="literal">static</span> Label <span class="ST1">addressBookCounter</span> = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Items in address book:</span><span class="string">&quot;</span>);
<span class="literal">static</span> Label <span class="ST1">indexLabel</span> = <span class="literal">new</span> Label();
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">start</span>(Stage stage) <span class="literal">throws</span> Exception
{
<span class="ST3">getData</span>();
BorderPane primaryBorderPane = <span class="literal">new</span> BorderPane();
GridPane textFieldGridPane = <span class="literal">new</span> GridPane();
GridPane addressFieldPane = <span class="literal">new</span> GridPane();
GridPane buttonFieldPane = <span class="literal">new</span> GridPane();
<span class="comment">// Text fields / labels</span>
textFieldGridPane.add(<span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Name</span><span class="string">&quot;</span>), <span class="number">0</span>, <span class="number">0</span>);
textFieldGridPane.add(<span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Street</span><span class="string">&quot;</span>), <span class="number">0</span>, <span class="number">1</span>);
textFieldGridPane.add(<span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">City</span><span class="string">&quot;</span>), <span class="number">0</span>, <span class="number">2</span>);
addressFieldPane.add(<span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">State</span><span class="string">&quot;</span>), <span class="number">2</span>, <span class="number">0</span>);
addressFieldPane.add(<span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">ZIP</span><span class="string">&quot;</span>), <span class="number">4</span>, <span class="number">0</span>);
textFieldGridPane.add(<span class="ST1">n</span><span class="ST1">ameTextField</span>, <span class="number">1</span>, <span class="number">0</span>);
textFieldGridPane.add(<span class="ST1">s</span><span class="ST1">treetTextField</span>, <span class="number">1</span>, <span class="number">1</span>);
textFieldGridPane.add(addressFieldPane, <span class="number">1</span>, <span class="number">2</span>);
addressFieldPane.add(<span class="ST1">c</span><span class="ST1">ityTextField</span>, <span class="number">1</span>, <span class="number">0</span>);
addressFieldPane.add(<span class="ST1">s</span><span class="ST1">tateTextField</span>, <span class="number">3</span>, <span class="number">0</span>);
addressFieldPane.add(<span class="ST1">z</span><span class="ST1">ipTextField</span>, <span class="number">5</span>, <span class="number">0</span>);
textFieldGridPane.getColumnConstraints().add(<span class="literal">new</span> ColumnConstraints(<span class="number">5</span><span class="number">0</span>));
primaryBorderPane.setTop(textFieldGridPane);
<span class="comment">// Buttons</span>
Button addButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Add</span><span class="string">&quot;</span>);
Button firstButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">First</span><span class="string">&quot;</span>);
Button nextButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Next</span><span class="string">&quot;</span>);
Button previousButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Previous</span><span class="string">&quot;</span>);
Button lastButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Last</span><span class="string">&quot;</span>);
Button updateButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Update</span><span class="string">&quot;</span>);
buttonFieldPane.add(<span class="ST1">i</span><span class="ST1">ndexLabel</span>, <span class="number">0</span>, <span class="number">0</span>);
buttonFieldPane.add(addButton, <span class="number">1</span>, <span class="number">0</span>);
buttonFieldPane.add(firstButton, <span class="number">2</span>, <span class="number">0</span>);
buttonFieldPane.add(nextButton, <span class="number">3</span>, <span class="number">0</span>);
buttonFieldPane.add(previousButton, <span class="number">4</span>, <span class="number">0</span>);
buttonFieldPane.add(lastButton, <span class="number">5</span>, <span class="number">0</span>);
buttonFieldPane.add(updateButton, <span class="number">6</span>, <span class="number">0</span>);
buttonFieldPane.add(<span class="ST1">a</span><span class="ST1">ddressBookCounter</span>, <span class="number">7</span>, <span class="number">0</span>);
primaryBorderPane.setBottom(buttonFieldPane);
buttonFieldPane.setAlignment(Pos.<span class="ST1">CENTER</span>);
buttonFieldPane.setHgap(<span class="number">1</span><span class="number">0</span>);
addButton.setOnAction(e -&gt; {
<span class="ST1">addressArray</span>.add(<span class="literal">new</span> AddressBookEntry());
<span class="ST3">updateData</span>();
});
firstButton.setOnAction(e -&gt; {
<span class="ST1">addressArrayPointer</span> = <span class="number">0</span>;
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
});
nextButton.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST1">addressArrayPointer</span> &gt;= <span class="ST1">addressArray</span>.size() - <span class="number">1</span>) {
<span class="ST1">addressArrayPointer</span> = <span class="number">0</span>;
} <span class="literal">else</span> {
<span class="ST1">addressArrayPointer</span>++;
}
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
});
previousButton.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST1">addressArrayPointer</span> &gt; <span class="number">0</span>) {
<span class="ST1">addressArrayPointer</span>--;
} <span class="literal">else</span> {
<span class="ST1">addressArrayPointer</span> = <span class="ST1">addressArray</span>.size() - <span class="number">1</span>;
}
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
});
lastButton.setOnAction(e -&gt; {
<span class="ST1">addressArrayPointer</span> = <span class="ST1">addressArray</span>.size() - <span class="number">1</span>;
<span class="ST3">getEntry</span>(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
});
updateButton.setOnAction(e -&gt; {
AddressBookEntry entry = <span class="ST1">addressArray</span>.get(<span class="ST1">a</span><span class="ST1">ddressArrayPointer</span>);
entry.setName(<span class="ST1">n</span><span class="ST1">ameTextField</span>.getText());
entry.setCity(<span class="ST1">n</span><span class="ST1">ameTextField</span>.getText());
entry.setStreet(<span class="ST1">s</span><span class="ST1">treetTextField</span>.getText());
entry.setCity(<span class="ST1">c</span><span class="ST1">ityTextField</span>.getText());
entry.setState(<span class="ST1">s</span><span class="ST1">tateTextField</span>.getText());
entry.setZip(<span class="ST1">z</span><span class="ST1">ipTextField</span>.getText());
<span class="ST3">updateData</span>();
});
<span class="comment">// init fields with data from first entry</span>
<span class="ST3">getEntry</span>(<span class="number">0</span>);
Scene scene = <span class="literal">new</span> Scene(primaryBorderPane);
stage.setScene(scene);
stage.show();
}
<span class="literal">public</span> <span class="literal">static</span> AddressBookEntry <span class="ST4">getEntry</span>(<span class="literal">int</span> index)
{
AddressBookEntry entry = <span class="ST1">addressArray</span>.get(index);
<span class="ST1">indexLabel</span>.setText(<span class="string">&quot;</span><span class="string">Current index: </span><span class="string">&quot;</span> + index);
<span class="ST1">addressBookCounter</span>.setText(<span class="string">&quot;</span><span class="string">Items in address book: </span><span class="string">&quot;</span> + <span class="ST1">addressArray</span>.size());
<span class="ST1">nameTextField</span>.setText(entry.getName());
<span class="ST1">streetTextField</span>.setText(entry.getStreet());
<span class="ST1">cityTextField</span>.setText(entry.getCity());
<span class="ST1">stateTextField</span>.setText(entry.getState());
<span class="ST1">zipTextField</span>.setText(entry.getZip());
<span class="literal">return</span> entry;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">updateData</span>()
{
<span class="literal">try</span> (ObjectOutputStream fileStream = <span class="literal">new</span> ObjectOutputStream(<span class="literal">new</span> FileOutputStream(<span class="ST1">a</span><span class="ST1">ddressBook</span>))) {
fileStream.writeObject(<span class="ST1">a</span><span class="ST1">ddressArray</span>);
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST1">out</span>.println(ex);
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">getData</span>()
{
<span class="literal">if</span> (!<span class="ST1">addressBook</span>.exists()) {
<span class="ST1">addressArray</span>.add(<span class="literal">new</span> AddressBookEntry());
<span class="ST3">updateData</span>();
} <span class="literal">else</span> {
<span class="literal">try</span> (ObjectInputStream fileStream = <span class="literal">new</span> ObjectInputStream(<span class="literal">new</span> FileInputStream(<span class="ST1">a</span><span class="ST1">ddressBook</span>))) {
<span class="ST1">addressArray</span> = (ArrayList&lt;AddressBookEntry&gt;) fileStream.readObject();
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST1">out</span>.println(ex);
} <span class="literal">catch</span> (ClassNotFoundException ex) {
System.<span class="ST1">out</span>.println(ex);
}
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">main</span>(String[] args)
{
<span class="ST3">launch</span>();
}
}
<span class="literal">class</span> AddressBookEntry <span class="literal">implements</span> Serializable {
<span class="literal">private</span> String <span class="ST5">name</span>;
<span class="literal">private</span> String <span class="ST5">street</span>;
<span class="literal">private</span> String <span class="ST5">city</span>;
<span class="literal">private</span> String <span class="ST5">state</span>;
<span class="literal">private</span> String <span class="ST5">zip</span>;
<span class="literal">public</span> AddressBookEntry()
{
<span class="literal">this</span>.<span class="ST5">name</span> = <span class="string">&quot;</span><span class="string">Enter a name here.</span><span class="string">&quot;</span>;
<span class="literal">this</span>.<span class="ST5">street</span> = <span class="string">&quot;</span><span class="string">Enter a street here.</span><span class="string">&quot;</span>;
<span class="literal">this</span>.<span class="ST5">city</span> = <span class="string">&quot;</span><span class="string">Enter a city here.</span><span class="string">&quot;</span>;
<span class="literal">this</span>.<span class="ST5">state</span> = <span class="string">&quot;</span><span class="string">Enter a state here.</span><span class="string">&quot;</span>;
<span class="literal">this</span>.<span class="ST5">zip</span> = <span class="string">&quot;</span><span class="string">Enter a zip here.</span><span class="string">&quot;</span>;
}
<span class="literal">public</span> AddressBookEntry(String name, String street, String city, String state, String zip)
{
<span class="literal">this</span>.<span class="ST5">name</span> = name;
<span class="literal">this</span>.<span class="ST5">street</span> = street;
<span class="literal">this</span>.<span class="ST5">city</span> = city;
<span class="literal">this</span>.<span class="ST5">state</span> = state;
<span class="literal">this</span>.<span class="ST5">zip</span> = zip;
}
<span class="literal">public</span> String <span class="ST2">getZip</span>()
{
<span class="literal">return</span> <span class="ST5">zip</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setZip</span>(String zip)
{
<span class="literal">this</span>.<span class="ST5">zip</span> = zip;
}
<span class="literal">public</span> String <span class="ST2">getState</span>()
{
<span class="literal">return</span> <span class="ST5">state</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setState</span>(String state)
{
<span class="literal">this</span>.<span class="ST5">state</span> = state;
}
<span class="literal">public</span> String <span class="ST2">getCity</span>()
{
<span class="literal">return</span> <span class="ST5">city</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setCity</span>(String city)
{
<span class="literal">this</span>.<span class="ST5">city</span> = city;
}
<span class="literal">public</span> String <span class="ST2">getStreet</span>()
{
<span class="literal">return</span> <span class="ST5">street</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setStreet</span>(String street)
{
<span class="literal">this</span>.<span class="ST5">street</span> = street;
}
<span class="literal">public</span> String <span class="ST2">getName</span>()
{
<span class="literal">return</span> <span class="ST5">name</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">setName</span>(String name)
{
<span class="literal">this</span>.<span class="ST5">name</span> = name;
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,160 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>CombineFilesFX.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST3 {color: #9876aa}
.ST4 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST1 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/CombineFilesFX.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">io</span><span class="comment">.</span><span class="comment">DataInput</span><span class="comment">;</span>
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.Collections;
<span class="literal">import</span> java.util.Comparator;
<span class="literal">import</span> java.util.List;
<span class="literal">import</span> java.util.regex.Matcher;
<span class="literal">import</span> java.util.regex.Pattern;
<span class="literal">import</span> javafx.application.Application;
<span class="literal">import</span> javafx.geometry.Pos;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.control.Alert;
<span class="literal">import</span> javafx.scene.control.Alert.<span class="ST1">AlertType</span>;
<span class="literal">import</span> javafx.scene.control.Button;
<span class="literal">import</span> javafx.scene.control.Label;
<span class="comment">import</span> <span class="comment">javafx</span><span class="comment">.</span><span class="comment">scene</span><span class="comment">.</span><span class="comment">control</span><span class="comment">.</span><span class="comment">TextField</span><span class="comment">;</span>
<span class="literal">import</span> javafx.scene.layout.BorderPane;
<span class="literal">import</span> javafx.scene.layout.GridPane;
<span class="literal">import</span> javafx.stage.FileChooser;
<span class="literal">import</span> javafx.stage.Stage;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> CombineFilesFX <span class="literal">extends</span> Application {
<span class="literal">static</span> ArrayList&lt;File&gt; <span class="ST2">files</span> = <span class="literal">new</span> ArrayList&lt;File&gt;();
<span class="literal">final</span> FileChooser <span class="ST3">fileChooser</span> = <span class="literal">new</span> FileChooser();
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">start</span>(<span class="literal">final</span> Stage stage) <span class="literal">throws</span> Exception {
<span class="ST3">fileChooser</span>.setTitle(<span class="string">&quot;</span><span class="string">Open File to Split...</span><span class="string">&quot;</span>);
BorderPane primaryBorderPane = <span class="literal">new</span> BorderPane();
GridPane textFieldGridPane = <span class="literal">new</span> GridPane();
Label infoLabel = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Point me at the files that were output by SplitFilesFX.java.</span><span class="string">&quot;</span>);
Label chooseFile = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Choose files to combine: </span><span class="string">&quot;</span>);
Button openFilePicker = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Choose...</span><span class="string">&quot;</span>);
Button run = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Start</span><span class="string">&quot;</span>);
textFieldGridPane.add(chooseFile, <span class="number">0</span>, <span class="number">0</span>);
textFieldGridPane.add(openFilePicker, <span class="number">1</span>, <span class="number">0</span>);
primaryBorderPane.<span class="ST1">setAlignment</span>(run, Pos.<span class="ST2">CENTER</span>);
primaryBorderPane.setTop(infoLabel);
primaryBorderPane.setCenter(textFieldGridPane);
primaryBorderPane.setBottom(run);
openFilePicker.setOnAction(e -&gt; {
<span class="comment">// When we initially get the files from the file chooser, it returns an unsortable list. Because of this, we need to create a new List with its data.</span>
<span class="ST2">files</span>.addAll(<span class="ST3">f</span><span class="ST3">ileChooser</span>.showOpenMultipleDialog(openFilePicker.getScene().getWindow()));
<span class="comment">// The file picker appears to return files in a random order, so we need to sort them by file name.</span>
Collections.<span class="ST1">sort</span>(<span class="ST2">f</span><span class="ST2">iles</span>, <span class="literal">new</span> Comparator&lt;File&gt;() {
<span class="literal">public</span> <span class="literal">int</span> <span class="ST4">compare</span>(File o1, File o2) {
<span class="literal">return</span> extractInt(o1.getName()) - extractInt(o2.getName());
}
String <span class="ST4">findMatch</span>(String s) {
Pattern findNum = Pattern.<span class="ST1">compile</span>(<span class="string">&quot;</span><span class="literal">\\</span><span class="string">d+$</span><span class="string">&quot;</span>);
Matcher match = findNum.matcher(s);
<span class="literal">while</span> (match.find()) {
<span class="literal">return</span> match.group();
}
<span class="literal">return</span> <span class="string">&quot;&quot;</span>;
}
<span class="literal">int</span> <span class="ST4">extractInt</span>(String s) {
String num = findMatch(s);
System.<span class="ST2">out</span>.println(num);
<span class="comment">// return 0 if no digits found</span>
<span class="literal">return</span> num.isEmpty() ? <span class="number">0</span> : Integer.<span class="ST1">parseInt</span>(num);
}
});
});
run.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST2">files</span> == <span class="literal">null</span>) {
e.consume();
}
<span class="ST1">combineFiles</span>(<span class="ST2">f</span><span class="ST2">iles</span>);
});
Scene scene = <span class="literal">new</span> Scene(primaryBorderPane);
stage.setScene(scene);
stage.show();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">combineFiles</span>(List&lt;File&gt; filesToCombine) {
String outputPath = filesToCombine.get(<span class="number">0</span>).getParent();
<span class="literal">try</span> (FileOutputStream dataOut = <span class="literal">new</span> FileOutputStream(outputPath + <span class="string">&quot;</span><span class="string">/</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">reconstructed_</span><span class="string">&quot;</span> + filesToCombine.get(<span class="number">0</span>).getName().substring(<span class="number">0</span>, (filesToCombine.get(<span class="number">0</span>).getName().length() - <span class="number">2</span>)))) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Writing to </span><span class="string">&quot;</span> + outputPath + <span class="string">&quot;</span><span class="string">/</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">reconstructed_</span><span class="string">&quot;</span> + filesToCombine.get(<span class="number">0</span>).getName().substring(<span class="number">0</span>, (filesToCombine.get(<span class="number">0</span>).getName().length() - <span class="number">2</span>)));
<span class="literal">for</span> (File file : filesToCombine) {
<span class="literal">try</span> (FileInputStream dataIn = <span class="literal">new</span> FileInputStream(file)) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Opening the source file </span><span class="string">&quot;</span> + file.getName() + <span class="string">&quot;</span><span class="string">!</span><span class="string">&quot;</span>);
<span class="literal">byte</span>[] buffer = <span class="literal">new</span> <span class="literal">byte</span>[<span class="number">4096</span>];
<span class="literal">int</span> bytesRead = -<span class="number">1</span>;
<span class="literal">while</span> ((bytesRead = dataIn.read(buffer)) != -<span class="number">1</span>) {
dataOut.write(buffer, <span class="number">0</span>, bytesRead);
}
}
<span class="literal">catch</span> (IOException ex) {
System.<span class="ST2">out</span>.println(ex);
}
}
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST2">out</span>.println(ex);
}
Alert alert = <span class="literal">new</span> Alert(<span class="ST1">A</span><span class="ST1">lertType</span>.<span class="ST2">INFORMATION</span>);
alert.setTitle(<span class="string">&quot;</span><span class="string">Success!</span><span class="string">&quot;</span>);
alert.setContentText(<span class="string">&quot;</span><span class="string">Successfully combined the files!</span><span class="string">&quot;</span>);
alert.showAndWait();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) {
<span class="ST1">launch</span>();
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,80 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Exercise17_01.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/Exercise17_01.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="literal">import</span> java.io.EOFException;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">io</span><span class="comment">.</span><span class="comment">FileNotFoundException</span><span class="comment">;</span>
<span class="literal">import</span> java.io.RandomAccessFile;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> Exercise17_01 {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args)
{
<span class="literal">try</span> (RandomAccessFile fileIO = <span class="literal">new</span> RandomAccessFile(<span class="string">&quot;</span><span class="string">Exercise17_01.txt</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">rw</span><span class="string">&quot;</span>)) {
<span class="literal">int</span> random = <span class="number">0</span>;
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Writing data to file...</span><span class="string">&quot;</span>);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">1</span>; i &lt; <span class="number">100</span> + <span class="number">1</span>; ++i) {
random = (<span class="literal">int</span>) (Math.<span class="ST3">random</span>() * <span class="number">9</span>) + <span class="number">1</span>;
System.<span class="ST2">out</span>.print(random + <span class="string">&quot;</span> <span class="string">&quot;</span>);
<span class="literal">if</span> (i != <span class="number">0</span> &amp;&amp; i % <span class="number">10</span> == <span class="number">0</span>) {
System.<span class="ST2">out</span>.println();
}
fileIO.writeInt(random);
}
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Wrote to the file successfully!</span><span class="string">&quot;</span>);
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">File contents:</span><span class="string">&quot;</span>);
fileIO.seek(<span class="number">0</span>);
<span class="literal">int</span> readIterator = <span class="number">1</span>;
<span class="literal">while</span> (<span class="literal">true</span>) {
<span class="literal">try</span> {
System.<span class="ST2">out</span>.print(fileIO.readInt() + <span class="string">&quot;</span> <span class="string">&quot;</span>);
<span class="literal">if</span> (readIterator != <span class="number">0</span> &amp;&amp; readIterator % <span class="number">10</span> == <span class="number">0</span>) {
System.<span class="ST2">out</span>.println();
}
++readIterator;
} <span class="literal">catch</span> (EOFException e) {
<span class="literal">break</span>;
}
}
} <span class="literal">catch</span> (Exception ex) {
System.<span class="ST2">out</span>.println(ex);
}
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,94 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Exercise17_03.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.number {color: #6897bb}
.string {color: #6a8759}
.comment {color: #808080}
.whitespace {color: #505050}
.literal {color: #cc7832}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/Exercise17_03.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license</span>
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="literal">import</span> java.io.DataInputStream;
<span class="literal">import</span> java.io.DataOutputStream;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> Exercise17_03 {
<span class="literal">static</span> <span class="literal">int</span> fileSize = Math.abs((<span class="literal">int</span>) (Math.random() * <span class="number">1024</span>));
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> writeData()
{
<span class="literal">try</span> (FileOutputStream fileWrite = <span class="literal">new</span> FileOutputStream(<span class="string">&quot;</span><span class="string">Exercise17_03.dat</span><span class="string">&quot;</span>)) {
<span class="comment">// Write a unspecified number of integers into the file.</span>
<span class="comment">// Must be positive!</span>
<span class="literal">int</span> randInt = <span class="number">0</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; fileSize; ++i) {
randInt = (<span class="literal">int</span>) (Math.random() * <span class="number">10</span>);
fileWrite.write(randInt);
}
} <span class="literal">catch</span> (IOException ex) {
System.out.println(ex);
}
System.out.println(<span class="string">&quot;</span><span class="string">Wrote data to the file!</span><span class="string">&quot;</span>);
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span>[] readData()
{
<span class="literal">int</span>[] fileData = <span class="literal">new</span> <span class="literal">int</span>[fileSize];
<span class="literal">try</span> (FileInputStream fileRead = <span class="literal">new</span> FileInputStream(<span class="string">&quot;</span><span class="string">Exercise17_03.dat</span><span class="string">&quot;</span>)) {
<span class="comment">// Read the data back</span>
<span class="literal">int</span> dataIterator = <span class="number">0</span>;
<span class="literal">int</span> dataStream = <span class="number">0</span>;
<span class="literal">while</span> (fileRead.available() &gt; <span class="number">0</span>) {
dataStream = fileRead.read();
fileData[dataIterator++] = dataStream;
System.out.print(dataStream + <span class="string">&quot;</span> <span class="string">&quot;</span>);
<span class="literal">if</span> ((dataIterator + <span class="number">1</span>) % <span class="number">10</span> == <span class="number">0</span>) {
System.out.println();
}
}
} <span class="literal">catch</span> (IOException ex) {
System.out.println(ex);
}
<span class="literal">return</span> fileData;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
{
System.out.println(<span class="string">&quot;</span><span class="string">Ints to write: </span><span class="string">&quot;</span> + fileSize);
writeData();
<span class="literal">int</span>[] fileData = readData();
<span class="comment">// Sum the digits</span>
<span class="literal">int</span> sum = <span class="number">0</span>;
<span class="literal">for</span> (<span class="literal">int</span> i: fileData) {
sum += i;
}
System.out.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">The sum of the integers in the file is: </span><span class="string">&quot;</span> + sum);
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,107 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Exercise17_05.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST3 {color: #9876aa}
.ST4 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/Exercise17_05.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">io</span><span class="comment">.</span><span class="comment">FileNotFoundException</span><span class="comment">;</span>
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="literal">import</span> java.io.ObjectInputStream;
<span class="literal">import</span> java.io.Serializable;
<span class="literal">import</span> java.io.ObjectOutputStream;
<span class="literal">import</span> java.util.Date;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> Exercise17_05 {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
File dataFile = <span class="literal">new</span> File(<span class="string">&quot;</span><span class="string">Exercise17_05.dat</span><span class="string">&quot;</span>);
<span class="literal">if</span>(!dataFile.exists()) {
<span class="literal">try</span> (ObjectOutputStream fileStream = <span class="literal">new</span> ObjectOutputStream(<span class="literal">new</span> FileOutputStream(dataFile))) {
fileStream.writeObject(<span class="literal">new</span> DataContainer());
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST2">out</span>.println(ex);
}
}
DataContainer data = <span class="literal">null</span>;
<span class="literal">try</span> (ObjectInputStream fileStream = <span class="literal">new</span> ObjectInputStream(<span class="literal">new</span> FileInputStream(dataFile))) {
data = (DataContainer) fileStream.readObject();
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST2">out</span>.println(ex);
} <span class="literal">catch</span> (ClassNotFoundException ex) {
System.<span class="ST2">out</span>.println(ex);
}
<span class="comment">// Now print out the data!</span>
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">We got the data from the file!</span><span class="string">&quot;</span>);
System.<span class="ST2">out</span>.println(data.toString());
}
}
<span class="literal">class</span> DataContainer <span class="literal">implements</span> Serializable {
<span class="literal">int</span>[] <span class="ST3">intArray</span> = {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>};
Date <span class="ST3">currentDate</span> = <span class="literal">new</span> Date();
<span class="literal">double</span> <span class="ST3">doubleMoment</span> = <span class="number">5.5</span>;
<span class="literal">public</span> <span class="literal">int</span>[] <span class="ST4">getIntArray</span>()
{
<span class="literal">return</span> <span class="ST3">intArray</span>;
}
<span class="literal">public</span> Date <span class="ST4">getCurrentDate</span>()
{
<span class="literal">return</span> <span class="ST3">currentDate</span>;
}
<span class="literal">public</span> <span class="literal">double</span> <span class="ST4">getDoubleMoment</span>()
{
<span class="literal">return</span> <span class="ST3">doubleMoment</span>;
}
@Override
<span class="literal">public</span> String <span class="ST4">toString</span>()
{
String intString = <span class="string">&quot;</span><span class="string">[</span><span class="string">&quot;</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST3">intArray</span>.<span class="ST3">length</span> - <span class="number">1</span>; ++i) {
intString += <span class="ST3">intArray</span>[i];
<span class="literal">if</span> (i == (<span class="ST3">intArray</span>.<span class="ST3">length</span> - <span class="number">2</span>)) {
intString += <span class="string">&quot;</span><span class="string">]</span><span class="string">&quot;</span>;
} <span class="literal">else</span> {
intString += <span class="string">&quot;</span><span class="string">, </span><span class="string">&quot;</span>;
}
}
<span class="literal">return</span> <span class="string">&quot;</span><span class="string">DataContainer{</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">intArray=</span><span class="string">&quot;</span> + intString + <span class="string">&quot;</span><span class="string">, currentDate=</span><span class="string">&quot;</span> + <span class="ST3">currentDate</span> + <span class="string">&quot;</span><span class="string">, doubleMoment=</span><span class="string">&quot;</span> + <span class="ST3">doubleMoment</span> + <span class="string">&#39;</span><span class="string">}</span><span class="string">&#39;</span>;
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,177 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>HexEditorFX.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST1 {color: #9876aa}
.ST3 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST4 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/HexEditorFX.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="literal">import</span> <span class="literal">static</span> com.calebfontenot.mp5.files_calebfontenot.SplitFilesFX.alert;
<span class="comment">import</span> <span class="comment">static</span> <span class="comment">com</span><span class="comment">.</span><span class="comment">calebfontenot</span><span class="comment">.</span><span class="comment">mp5</span><span class="comment">.</span><span class="comment">files_calebfontenot</span><span class="comment">.</span><span class="comment">SplitFilesFX</span><span class="comment">.</span><span class="comment">file</span><span class="comment">;</span>
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileNotFoundException;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">ArrayList</span><span class="comment">;</span>
<span class="literal">import</span> javafx.application.Application;
<span class="literal">import</span> javafx.geometry.Pos;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.control.Button;
<span class="literal">import</span> javafx.scene.control.Label;
<span class="literal">import</span> javafx.scene.control.TextArea;
<span class="literal">import</span> javafx.scene.layout.BorderPane;
<span class="literal">import</span> javafx.scene.layout.GridPane;
<span class="literal">import</span> javafx.stage.FileChooser;
<span class="literal">import</span> javafx.stage.Stage;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> HexEditorFX <span class="literal">extends</span> Application {
<span class="literal">final</span> FileChooser <span class="ST1">fileChooser</span> = <span class="literal">new</span> FileChooser();
<span class="literal">static</span> File <span class="ST2">file</span> = <span class="literal">null</span>;
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">start</span>(Stage stage) <span class="literal">throws</span> Exception {
Label label = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Open a file...</span><span class="string">&quot;</span>);
Button openFileButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Choose...</span><span class="string">&quot;</span>);
TextArea editorWindow = <span class="literal">new</span> TextArea();
Button saveFileButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Save</span><span class="string">&quot;</span>);
editorWindow.setWrapText(<span class="literal">t</span><span class="literal">rue</span>);
BorderPane bp = <span class="literal">new</span> BorderPane();
GridPane openFileGridPane = <span class="literal">new</span> GridPane();
openFileGridPane.add(label, <span class="number">0</span>, <span class="number">0</span>);
openFileGridPane.add(openFileButton, <span class="number">1</span>, <span class="number">0</span>);
bp.setTop(openFileGridPane);
bp.setCenter(editorWindow);
bp.setBottom(saveFileButton);
bp.<span class="ST4">setAlignment</span>(saveFileButton, Pos.<span class="ST2">CENTER</span>);
openFileButton.setOnAction(e -&gt; {
<span class="ST2">file</span> = <span class="ST1">fileChooser</span>.showOpenDialog(openFileButton.getScene().getWindow());
<span class="literal">byte</span>[] data = <span class="literal">null</span>;
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Getting data from fileand encoding it as hex...</span><span class="string">&quot;</span>);
<span class="literal">try</span> (FileInputStream dataIn = <span class="literal">new</span> FileInputStream(<span class="ST2">file</span>)) {
data = dataIn.readAllBytes();
} <span class="literal">catch</span> (FileNotFoundException ex) {
ex.printStackTrace();
} <span class="literal">catch</span> (IOException ex) {
ex.printStackTrace();
}
String dataString = encodeHexString(data);
editorWindow.setText(dataString);
});
saveFileButton.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST2">file</span> == <span class="literal">null</span>) {
<span class="ST2">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">No file selected!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setHeaderText(<span class="string">&quot;</span><span class="string">No file selected!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">You have to open a file first, silly!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.showAndWait();
}
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Re-encoding hex back into a byte array...</span><span class="string">&quot;</span>);
<span class="literal">try</span> (FileOutputStream dataOut = <span class="literal">new</span> FileOutputStream(<span class="ST2">file</span>)) {
<span class="literal">byte</span>[] bytesToSave = decodeHexString(editorWindow.getText());
dataOut.write(bytesToSave);
} <span class="literal">catch</span> (FileNotFoundException ex) {
ex.printStackTrace();
} <span class="literal">catch</span> (IOException ex) {
ex.printStackTrace();
} <span class="literal">catch</span> (IllegalArgumentException ex) {
<span class="ST2">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">Invalid Hex!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">Invalid hex entered into the text box, unable to save.</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.showAndWait();
}
<span class="ST2">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">Save successful!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">File saved successfully.</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.showAndWait();
});
Scene scene = <span class="literal">new</span> Scene(bp);
stage.setScene(scene);
stage.show();
}
<span class="literal">private</span> <span class="literal">int</span> <span class="ST3">toDigit</span>(<span class="literal">char</span> hexChar) {
<span class="literal">int</span> digit = Character.<span class="ST4">digit</span>(hexChar, <span class="number">1</span><span class="number">6</span>);
<span class="literal">if</span> (digit == -<span class="number">1</span>) {
<span class="literal">throw</span> <span class="literal">new</span> IllegalArgumentException(
<span class="string">&quot;</span><span class="string">Invalid Hexadecimal Character: </span><span class="string">&quot;</span> + hexChar);
}
<span class="literal">return</span> digit;
}
<span class="literal">public</span> <span class="literal">byte</span> <span class="ST3">hexToByte</span>(String hexString) {
<span class="literal">int</span> firstDigit = toDigit(hexString.charAt(<span class="number">0</span>));
<span class="literal">int</span> secondDigit = toDigit(hexString.charAt(<span class="number">1</span>));
<span class="literal">return</span> (<span class="literal">byte</span>) ((firstDigit &lt;&lt; <span class="number">4</span>) + secondDigit);
}
<span class="literal">public</span> <span class="literal">byte</span>[] <span class="ST3">decodeHexString</span>(String hexString) {
<span class="literal">if</span> (hexString.length() % <span class="number">2</span> == <span class="number">1</span>) {
<span class="literal">throw</span> <span class="literal">new</span> IllegalArgumentException(
<span class="string">&quot;</span><span class="string">Invalid hexadecimal String supplied.</span><span class="string">&quot;</span>);
}
<span class="literal">byte</span>[] bytes = <span class="literal">new</span> <span class="literal">byte</span>[hexString.length() / <span class="number">2</span>];
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; hexString.length(); i += <span class="number">2</span>) {
bytes[i / <span class="number">2</span>] = hexToByte(hexString.substring(i, i + <span class="number">2</span>));
}
<span class="literal">return</span> bytes;
}
<span class="literal">public</span> String <span class="ST3">byteToHex</span>(<span class="literal">byte</span> num) {
<span class="literal">char</span>[] hexDigits = <span class="literal">new</span> <span class="literal">char</span>[<span class="number">2</span>];
hexDigits[<span class="number">0</span>] = Character.<span class="ST4">forDigit</span>((num &gt;&gt; <span class="number">4</span>) &amp; <span class="number">0xF</span>, <span class="number">1</span><span class="number">6</span>);
hexDigits[<span class="number">1</span>] = Character.<span class="ST4">forDigit</span>((num &amp; <span class="number">0xF</span>), <span class="number">1</span><span class="number">6</span>);
<span class="literal">return</span> <span class="literal">new</span> String(hexDigits);
}
<span class="literal">public</span> String <span class="ST3">encodeHexString</span>(<span class="literal">byte</span>[] byteArray) {
StringBuffer hexStringBuffer = <span class="literal">new</span> StringBuffer();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; byteArray.<span class="ST1">length</span>; i++) {
hexStringBuffer.append(byteToHex(byteArray[i]));
}
<span class="literal">return</span> hexStringBuffer.toString();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) {
<span class="ST4">launch</span>(args);
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,152 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>SplitFilesFX.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST2 {color: #9876aa}
.ST4 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/SplitFilesFX.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">io</span><span class="comment">.</span><span class="comment">DataInput</span><span class="comment">;</span>
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="literal">import</span> javafx.application.Application;
<span class="literal">import</span> javafx.geometry.Pos;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.control.Alert;
<span class="literal">import</span> javafx.scene.control.Button;
<span class="literal">import</span> javafx.scene.control.Label;
<span class="literal">import</span> javafx.scene.control.ProgressBar;
<span class="literal">import</span> javafx.scene.control.TextField;
<span class="literal">import</span> javafx.scene.layout.BorderPane;
<span class="literal">import</span> javafx.scene.layout.GridPane;
<span class="literal">import</span> javafx.stage.FileChooser;
<span class="literal">import</span> javafx.stage.Stage;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> SplitFilesFX <span class="literal">extends</span> Application {
<span class="literal">static</span> File <span class="ST1">file</span> = <span class="literal">null</span>;
<span class="literal">final</span> FileChooser <span class="ST2">fileChooser</span> = <span class="literal">new</span> FileChooser();
<span class="literal">static</span> ProgressBar <span class="ST1">pb</span> = <span class="literal">new</span> ProgressBar();
<span class="literal">static</span> Alert <span class="ST1">alert</span> = <span class="literal">new</span> Alert(Alert.<span class="ST3">AlertType</span>.<span class="ST1">INFORMATION</span>);
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">start</span>(<span class="literal">final</span> Stage stage) <span class="literal">throws</span> Exception
{
<span class="ST2">fileChooser</span>.setTitle(<span class="string">&quot;</span><span class="string">Open File to Split...</span><span class="string">&quot;</span>);
BorderPane primaryBorderPane = <span class="literal">new</span> BorderPane();
BorderPane secondaryBoarderPane = <span class="literal">new</span> BorderPane();
GridPane textFieldGridPane = <span class="literal">new</span> GridPane();
<span class="ST1">pb</span>.prefWidthProperty().bind(stage.widthProperty());
Label infoLabel = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">If you split a file named tmp into 3 smaller files,</span><span class="literal">\n</span><span class="string"> the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3. </span><span class="string">&quot;</span>);
Label chooseFile = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Choose a file to split: </span><span class="string">&quot;</span>);
Button openFilePicker = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Choose...</span><span class="string">&quot;</span>);
Label splitCountLabel = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Enter the amount of files to split into: </span><span class="string">&quot;</span>);
TextField splitCount = <span class="literal">new</span> TextField();
Button run = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Start</span><span class="string">&quot;</span>);
textFieldGridPane.add(chooseFile, <span class="number">0</span>, <span class="number">0</span>);
textFieldGridPane.add(openFilePicker, <span class="number">1</span>, <span class="number">0</span>);
textFieldGridPane.add(splitCountLabel, <span class="number">0</span>, <span class="number">1</span>);
textFieldGridPane.add(splitCount, <span class="number">1</span>, <span class="number">1</span>);
secondaryBoarderPane.<span class="ST3">setAlignment</span>(run, Pos.<span class="ST1">CENTER</span>);
primaryBorderPane.setTop(infoLabel);
primaryBorderPane.setCenter(textFieldGridPane);
secondaryBoarderPane.setBottom(run);
secondaryBoarderPane.setTop(<span class="ST1">p</span><span class="ST1">b</span>);
primaryBorderPane.setBottom(secondaryBoarderPane);
openFilePicker.setOnAction(e -&gt; {
<span class="ST1">file</span> = <span class="ST2">fileChooser</span>.showOpenDialog(openFilePicker.getScene().getWindow());
});
run.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST1">file</span> == <span class="literal">null</span>) {
e.consume();
}
<span class="ST3">splitFile</span>(<span class="ST1">f</span><span class="ST1">ile</span>, Integer.<span class="ST3">parseInt</span>(splitCount.getText()));
});
Scene scene = <span class="literal">new</span> Scene(primaryBorderPane);
stage.setScene(scene);
stage.show();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">splitFile</span>(File fileToSplit, <span class="literal">int</span> splitCount) {
<span class="literal">int</span> outputFileSize = (<span class="literal">int</span>) fileToSplit.length() / splitCount;
System.<span class="ST1">out</span>.println(<span class="string">&quot;</span><span class="string">output file size will be: </span><span class="string">&quot;</span> + outputFileSize);
<span class="literal">if</span> (outputFileSize &lt; <span class="number">0</span>) {
<span class="ST1">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">Output files too large</span><span class="string">&quot;</span>);
<span class="ST1">alert</span>.setHeaderText(<span class="string">&quot;</span><span class="string">Output files too large</span><span class="string">&quot;</span>);
<span class="ST1">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">Please increase the amount of files to split!</span><span class="string">&quot;</span>);
<span class="ST1">alert</span>.showAndWait();
<span class="literal">return</span>;
}
<span class="literal">double</span> progress = <span class="number">0</span>;
String outputPath = fileToSplit.getParent();
<span class="comment">// Open the original file</span>
<span class="literal">try</span> (FileInputStream dataIn = <span class="literal">new</span> FileInputStream(fileToSplit)) {
System.<span class="ST1">out</span>.println(<span class="string">&quot;</span><span class="string">Opening the source file!</span><span class="string">&quot;</span>);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; splitCount; ++i) {
progress = (i * <span class="number">100</span>) / splitCount;
System.<span class="ST1">out</span>.println(progress);
<span class="ST1">pb</span>.setProgress(progress);
dataIn.mark(outputFileSize * (i + <span class="number">1</span>));
<span class="literal">try</span> (FileOutputStream dataOut = <span class="literal">new</span> FileOutputStream(outputPath + <span class="string">&quot;</span><span class="string">/</span><span class="string">&quot;</span> + fileToSplit.getName() + <span class="string">&quot;</span><span class="string">.</span><span class="string">&quot;</span> + (i + <span class="number">1</span>))) {
dataOut.write(dataIn.readNBytes(outputFileSize), <span class="number">0</span>, outputFileSize);
System.<span class="ST1">out</span>.println(<span class="string">&quot;</span><span class="string">Writing to </span><span class="string">&quot;</span> + outputPath + <span class="string">&quot;</span><span class="string">/</span><span class="string">&quot;</span> + fileToSplit.getName() + <span class="string">&quot;</span><span class="string">.</span><span class="string">&quot;</span> + (i + <span class="number">1</span>));
}
}
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST1">out</span>.println(ex);
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args)
{
<span class="ST3">launch</span>();
}
}
<span class="comment">/*</span>
<span class="comment"> */</span>
</pre></body>
</html>

View File

@@ -119,7 +119,43 @@ is divided into following sections:
<property name="module.name" value=""/> <property name="module.name" value=""/>
</target> </target>
<target depends="-pre-init,-init-private,-init-user,-init-project,-init-macrodef-property,-init-modules-supported" name="-do-init"> <target depends="-pre-init,-init-private,-init-user,-init-project,-init-macrodef-property,-init-modules-supported" name="-do-init">
<property name="platform.java" value="${java.home}/bin/java"/> <j2seproject1:property name="platform.home" value="platforms.${platform.active}.home"/>
<j2seproject1:property name="platform.bootcp" value="platforms.${platform.active}.bootclasspath"/>
<j2seproject1:property name="platform.compiler" value="platforms.${platform.active}.compile"/>
<j2seproject1:property name="platform.javac.tmp" value="platforms.${platform.active}.javac"/>
<condition property="platform.javac" value="${platform.home}/bin/javac">
<equals arg1="${platform.javac.tmp}" arg2="$${platforms.${platform.active}.javac}"/>
</condition>
<property name="platform.javac" value="${platform.javac.tmp}"/>
<j2seproject1:property name="platform.java.tmp" value="platforms.${platform.active}.java"/>
<condition property="platform.java" value="${platform.home}/bin/java">
<equals arg1="${platform.java.tmp}" arg2="$${platforms.${platform.active}.java}"/>
</condition>
<property name="platform.java" value="${platform.java.tmp}"/>
<j2seproject1:property name="platform.javadoc.tmp" value="platforms.${platform.active}.javadoc"/>
<condition property="platform.javadoc" value="${platform.home}/bin/javadoc">
<equals arg1="${platform.javadoc.tmp}" arg2="$${platforms.${platform.active}.javadoc}"/>
</condition>
<property name="platform.javadoc" value="${platform.javadoc.tmp}"/>
<condition property="platform.invalid" value="true">
<or>
<contains string="${platform.javac}" substring="$${platforms."/>
<contains string="${platform.java}" substring="$${platforms."/>
<contains string="${platform.javadoc}" substring="$${platforms."/>
</or>
</condition>
<fail unless="platform.home">Must set platform.home</fail>
<fail unless="platform.bootcp">Must set platform.bootcp</fail>
<fail unless="platform.java">Must set platform.java</fail>
<fail unless="platform.javac">Must set platform.javac</fail>
<fail if="platform.invalid">
The J2SE Platform is not correctly set up.
Your active platform is: ${platform.active}, but the corresponding property "platforms.${platform.active}.home" is not found in the project's properties files.
Either open the project in the IDE and setup the Platform with the same name or add it manually.
For example like this:
ant -Duser.properties.file=&lt;path_to_property_file&gt; jar (where you put the property "platforms.${platform.active}.home" in a .properties file)
or ant -Dplatforms.${platform.active}.home=&lt;path_to_JDK_home&gt; jar (where no properties file is used)
</fail>
<available file="${manifest.file}" property="manifest.available"/> <available file="${manifest.file}" property="manifest.available"/>
<condition property="splashscreen.available"> <condition property="splashscreen.available">
<and> <and>
@@ -242,20 +278,6 @@ is divided into following sections:
<condition else="" property="javac.profile.cmd.line.arg" value="-profile ${javac.profile}"> <condition else="" property="javac.profile.cmd.line.arg" value="-profile ${javac.profile}">
<isset property="profile.available"/> <isset property="profile.available"/>
</condition> </condition>
<condition else="false" property="jdkBug6558476">
<and>
<matches pattern="1\.[56]" string="${java.specification.version}"/>
<not>
<os family="unix"/>
</not>
</and>
</condition>
<condition else="false" property="javac.fork">
<or>
<istrue value="${jdkBug6558476}"/>
<istrue value="${javac.external.vm}"/>
</or>
</condition>
<property name="jar.index" value="false"/> <property name="jar.index" value="false"/>
<property name="jar.index.metainf" value="${jar.index}"/> <property name="jar.index.metainf" value="${jar.index}"/>
<property name="copylibs.rebase" value="true"/> <property name="copylibs.rebase" value="true"/>
@@ -343,7 +365,7 @@ is divided into following sections:
</path> </path>
</resourcecount> </resourcecount>
</condition> </condition>
<javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" fork="${javac.fork}" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}"> <javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" executable="${platform.javac}" fork="yes" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}">
<src> <src>
<dirset dir="@{gensrcdir}" erroronmissingdir="false"> <dirset dir="@{gensrcdir}" erroronmissingdir="false">
<include name="*"/> <include name="*"/>
@@ -394,7 +416,7 @@ is divided into following sections:
<property location="${build.dir}/empty" name="empty.dir"/> <property location="${build.dir}/empty" name="empty.dir"/>
<mkdir dir="${empty.dir}"/> <mkdir dir="${empty.dir}"/>
<mkdir dir="@{apgeneratedsrcdir}"/> <mkdir dir="@{apgeneratedsrcdir}"/>
<javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" fork="${javac.fork}" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}"> <javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" executable="${platform.javac}" fork="yes" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}">
<src> <src>
<dirset dir="@{gensrcdir}" erroronmissingdir="false"> <dirset dir="@{gensrcdir}" erroronmissingdir="false">
<include name="*"/> <include name="*"/>
@@ -436,7 +458,7 @@ is divided into following sections:
<sequential> <sequential>
<property location="${build.dir}/empty" name="empty.dir"/> <property location="${build.dir}/empty" name="empty.dir"/>
<mkdir dir="${empty.dir}"/> <mkdir dir="${empty.dir}"/>
<javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" fork="${javac.fork}" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}"> <javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" executable="${platform.javac}" fork="yes" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}">
<src> <src>
<dirset dir="@{gensrcdir}" erroronmissingdir="false"> <dirset dir="@{gensrcdir}" erroronmissingdir="false">
<include name="*"/> <include name="*"/>
@@ -515,7 +537,7 @@ is divided into following sections:
<element name="customizePrototype" optional="true"/> <element name="customizePrototype" optional="true"/>
<sequential> <sequential>
<property name="junit.forkmode" value="perTest"/> <property name="junit.forkmode" value="perTest"/>
<junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" showoutput="true" tempdir="${build.dir}"> <junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" jvm="${platform.java}" showoutput="true" tempdir="${build.dir}">
<syspropertyset> <syspropertyset>
<propertyref prefix="test-sys-prop."/> <propertyref prefix="test-sys-prop."/>
<mapper from="test-sys-prop.*" to="*" type="glob"/> <mapper from="test-sys-prop.*" to="*" type="glob"/>
@@ -543,7 +565,7 @@ is divided into following sections:
<element name="customizePrototype" optional="true"/> <element name="customizePrototype" optional="true"/>
<sequential> <sequential>
<property name="junit.forkmode" value="perTest"/> <property name="junit.forkmode" value="perTest"/>
<junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" showoutput="true" tempdir="${build.dir}"> <junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="${junit.forkmode}" jvm="${platform.java}" showoutput="true" tempdir="${build.dir}">
<syspropertyset> <syspropertyset>
<propertyref prefix="test-sys-prop."/> <propertyref prefix="test-sys-prop."/>
<mapper from="test-sys-prop.*" to="*" type="glob"/> <mapper from="test-sys-prop.*" to="*" type="glob"/>
@@ -619,7 +641,7 @@ is divided into following sections:
</fileset> </fileset>
</union> </union>
<taskdef classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}" name="testng"/> <taskdef classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}" name="testng"/>
<testng classfilesetref="test.set" failureProperty="tests.failed" listeners="org.testng.reporters.VerboseReporter" methods="${testng.methods.arg}" mode="${testng.mode}" outputdir="${build.test.results.dir}" suitename="MP5-Binary-Files_CalebFontenot" testname="TestNG tests" workingDir="${work.dir}"> <testng classfilesetref="test.set" failureProperty="tests.failed" jvm="${platform.java}" listeners="org.testng.reporters.VerboseReporter" methods="${testng.methods.arg}" mode="${testng.mode}" outputdir="${build.test.results.dir}" suitename="MP5-Binary-Files_CalebFontenot" testname="TestNG tests" workingDir="${work.dir}">
<xmlfileset dir="${build.test.classes.dir}" includes="@{testincludes}"/> <xmlfileset dir="${build.test.classes.dir}" includes="@{testincludes}"/>
<propertyset> <propertyset>
<propertyref prefix="test-sys-prop."/> <propertyref prefix="test-sys-prop."/>
@@ -850,6 +872,9 @@ is divided into following sections:
<classpath> <classpath>
<path path="@{classpath}"/> <path path="@{classpath}"/>
</classpath> </classpath>
<bootclasspath>
<path path="${platform.bootcp}"/>
</bootclasspath>
</nbjpdastart> </nbjpdastart>
</sequential> </sequential>
</macrodef> </macrodef>
@@ -899,7 +924,7 @@ is divided into following sections:
<attribute default="jvm" name="jvm"/> <attribute default="jvm" name="jvm"/>
<element name="customize" optional="true"/> <element name="customize" optional="true"/>
<sequential> <sequential>
<java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true" module="@{modulename}"> <java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true" jvm="${platform.java}" module="@{modulename}">
<classpath> <classpath>
<path path="@{classpath}"/> <path path="@{classpath}"/>
</classpath> </classpath>
@@ -933,7 +958,7 @@ is divided into following sections:
<attribute default="jvm" name="jvm"/> <attribute default="jvm" name="jvm"/>
<element name="customize" optional="true"/> <element name="customize" optional="true"/>
<sequential> <sequential>
<java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true"> <java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true" jvm="${platform.java}">
<classpath> <classpath>
<path path="@{classpath}"/> <path path="@{classpath}"/>
</classpath> </classpath>
@@ -965,7 +990,7 @@ is divided into following sections:
<attribute default="jvm" name="jvm"/> <attribute default="jvm" name="jvm"/>
<element name="customize" optional="true"/> <element name="customize" optional="true"/>
<sequential> <sequential>
<java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true"> <java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true" jvm="${platform.java}">
<jvmarg line="${endorsed.classpath.cmd.line.arg}"/> <jvmarg line="${endorsed.classpath.cmd.line.arg}"/>
<jvmarg value="-Dfile.encoding=${runtime.encoding}"/> <jvmarg value="-Dfile.encoding=${runtime.encoding}"/>
<redirector errorencoding="${runtime.encoding}" inputencoding="${runtime.encoding}" outputencoding="${runtime.encoding}"/> <redirector errorencoding="${runtime.encoding}" inputencoding="${runtime.encoding}" outputencoding="${runtime.encoding}"/>
@@ -1199,7 +1224,7 @@ is divided into following sections:
<j2seproject3:copylibs manifest="${tmp.manifest.file}"/> <j2seproject3:copylibs manifest="${tmp.manifest.file}"/>
<echo level="info">To run this application from the command line without Ant, try:</echo> <echo level="info">To run this application from the command line without Ant, try:</echo>
<property location="${dist.jar}" name="dist.jar.resolved"/> <property location="${dist.jar}" name="dist.jar.resolved"/>
<echo level="info">java -jar "${dist.jar.resolved}"</echo> <echo level="info">${platform.java} -jar "${dist.jar.resolved}"</echo>
</target> </target>
<target depends="init,compile,-pre-pre-jar,-pre-jar,-do-jar-create-manifest,-do-jar-copy-manifest,-do-jar-set-mainclass,-do-jar-set-profile,-do-jar-set-splashscreen,-check-do-mkdist" if="do.archive" name="-do-jar-jar" unless="do.mkdist"> <target depends="init,compile,-pre-pre-jar,-pre-jar,-do-jar-create-manifest,-do-jar-copy-manifest,-do-jar-set-mainclass,-do-jar-set-profile,-do-jar-set-splashscreen,-check-do-mkdist" if="do.archive" name="-do-jar-jar" unless="do.mkdist">
<j2seproject1:jar manifest="${tmp.manifest.file}"/> <j2seproject1:jar manifest="${tmp.manifest.file}"/>
@@ -1301,8 +1326,8 @@ is divided into following sections:
<isset property="main.class.available"/> <isset property="main.class.available"/>
</and> </and>
</condition> </condition>
<property name="platform.jlink" value="${jdk.home}/bin/jlink"/> <property name="platform.jlink" value="${platform.home}/bin/jlink"/>
<property name="jlink.systemmodules.internal" value="${jdk.home}/jmods"/> <property name="jlink.systemmodules.internal" value="${platform.home}/jmods"/>
<exec executable="${platform.jlink}"> <exec executable="${platform.jlink}">
<arg value="--module-path"/> <arg value="--module-path"/>
<arg path="${jlink.systemmodules.internal}:${run.modulepath}:${dist.jar}"/> <arg path="${jlink.systemmodules.internal}:${run.modulepath}:${dist.jar}"/>
@@ -1495,16 +1520,19 @@ is divided into following sections:
</not> </not>
</and> </and>
</condition> </condition>
<exec executable="${platform.java}" failonerror="false" outputproperty="platform.version.output">
<arg value="-version"/>
</exec>
<condition else="" property="bug5101868workaround" value="*.java"> <condition else="" property="bug5101868workaround" value="*.java">
<matches pattern="1\.[56](\..*)?" string="${java.version}"/> <matches multiline="true" pattern="1\.[56](\..*)?" string="${platform.version.output}"/>
</condition> </condition>
<condition else="" property="javadoc.html5.cmd.line.arg" value="-html5"> <condition else="" property="javadoc.html5.cmd.line.arg" value="-html5">
<and> <and>
<isset property="javadoc.html5"/> <isset property="javadoc.html5"/>
<available file="${jdk.home}${file.separator}lib${file.separator}jrt-fs.jar"/> <available file="${platform.home}${file.separator}lib${file.separator}jrt-fs.jar"/>
</and> </and>
</condition> </condition>
<javadoc additionalparam="-J-Dfile.encoding=${file.encoding} ${javadoc.additionalparam}" author="${javadoc.author}" charset="UTF-8" destdir="${dist.javadoc.dir}" docencoding="UTF-8" encoding="${javadoc.encoding.used}" failonerror="true" noindex="${javadoc.noindex}" nonavbar="${javadoc.nonavbar}" notree="${javadoc.notree}" private="${javadoc.private}" source="${javac.source}" splitindex="${javadoc.splitindex}" use="${javadoc.use}" useexternalfile="true" version="${javadoc.version}" windowtitle="${javadoc.windowtitle}"> <javadoc additionalparam="-J-Dfile.encoding=${file.encoding} ${javadoc.additionalparam}" author="${javadoc.author}" charset="UTF-8" destdir="${dist.javadoc.dir}" docencoding="UTF-8" encoding="${javadoc.encoding.used}" executable="${platform.javadoc}" failonerror="true" noindex="${javadoc.noindex}" nonavbar="${javadoc.nonavbar}" notree="${javadoc.notree}" private="${javadoc.private}" source="${javac.source}" splitindex="${javadoc.splitindex}" use="${javadoc.use}" useexternalfile="true" version="${javadoc.version}" windowtitle="${javadoc.windowtitle}">
<classpath> <classpath>
<path path="${javac.classpath}"/> <path path="${javac.classpath}"/>
</classpath> </classpath>

View File

@@ -1,8 +1,8 @@
build.xml.data.CRC32=a0844a83 build.xml.data.CRC32=5b3e797d
build.xml.script.CRC32=dee8d968 build.xml.script.CRC32=dee8d968
build.xml.stylesheet.CRC32=f85dc8f2@1.108.0.48 build.xml.stylesheet.CRC32=f85dc8f2@1.108.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=a0844a83 nbproject/build-impl.xml.data.CRC32=5b3e797d
nbproject/build-impl.xml.script.CRC32=025ed557 nbproject/build-impl.xml.script.CRC32=7cb0beee
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.108.0.48 nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.108.0.48

View File

@@ -49,7 +49,7 @@ javac.compilerargs=
javac.deprecation=false javac.deprecation=false
javac.external.vm=true javac.external.vm=true
javac.modulepath=\ javac.modulepath=\
${libs.JavaFX20.classpath} ${libs.JavaFX_20.classpath}
javac.processormodulepath= javac.processormodulepath=
javac.processorpath=\ javac.processorpath=\
${javac.classpath} ${javac.classpath}
@@ -101,7 +101,7 @@ manifest.custom.permissions=
manifest.file=manifest.mf manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false mkdist.disabled=false
platform.active=default_platform platform.active=Graal_JDK_20
run.classpath=\ run.classpath=\
${javac.classpath}:\ ${javac.classpath}:\
${build.classes.dir}:\ ${build.classes.dir}:\
@@ -109,7 +109,7 @@ run.classpath=\
# Space-separated list of JVM arguments used when running the project. # Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value: # To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=-XX:+UnlockExperimentalVMOptions -XX:+EagerJVMCI -XX:+EnableJVMCI -XX:+UseJVMCICompiler -XX:+UseJVMCINativeLibrary run.jvmargs=--module-path=/usr/lib --add-modules javafx.controls -XX:+UnlockExperimentalVMOptions -XX:+EagerJVMCI -XX:+EnableJVMCI -XX:+UseJVMCICompiler -XX:+UseJVMCINativeLibrary
run.modulepath=\ run.modulepath=\
${javac.modulepath} ${javac.modulepath}
run.test.classpath=\ run.test.classpath=\

View File

@@ -4,6 +4,7 @@
<configuration> <configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3"> <data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>MP5-Binary-Files_CalebFontenot</name> <name>MP5-Binary-Files_CalebFontenot</name>
<explicit-platform explicit-source-supported="true"/>
<source-roots> <source-roots>
<root id="src.dir"/> <root id="src.dir"/>
</source-roots> </source-roots>

View File

@@ -10,10 +10,16 @@ import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.application.Application; import javafx.application.Application;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
@@ -28,12 +34,11 @@ import javafx.stage.Stage;
*/ */
public class CombineFilesFX extends Application { public class CombineFilesFX extends Application {
static List<File> files = null; static ArrayList<File> files = new ArrayList<File>();
final FileChooser fileChooser = new FileChooser(); final FileChooser fileChooser = new FileChooser();
@Override @Override
public void start(final Stage stage) throws Exception public void start(final Stage stage) throws Exception {
{
fileChooser.setTitle("Open File to Split..."); fileChooser.setTitle("Open File to Split...");
BorderPane primaryBorderPane = new BorderPane(); BorderPane primaryBorderPane = new BorderPane();
GridPane textFieldGridPane = new GridPane(); GridPane textFieldGridPane = new GridPane();
@@ -51,7 +56,30 @@ public class CombineFilesFX extends Application {
primaryBorderPane.setBottom(run); primaryBorderPane.setBottom(run);
openFilePicker.setOnAction(e -> { openFilePicker.setOnAction(e -> {
files = fileChooser.showOpenMultipleDialog(openFilePicker.getScene().getWindow()); // When we initially get the files from the file chooser, it returns an unsortable list. Because of this, we need to create a new List with its data.
files.addAll(fileChooser.showOpenMultipleDialog(openFilePicker.getScene().getWindow()));
// The file picker appears to return files in a random order, so we need to sort them by file name.
Collections.sort(files, new Comparator<File>() {
public int compare(File o1, File o2) {
return extractInt(o1.getName()) - extractInt(o2.getName());
}
String findMatch(String s) {
Pattern findNum = Pattern.compile("\\d+$");
Matcher match = findNum.matcher(s);
while (match.find()) {
return match.group();
}
return "";
}
int extractInt(String s) {
String num = findMatch(s);
System.out.println(num);
// return 0 if no digits found
return num.isEmpty() ? 0 : Integer.parseInt(num);
}
});
}); });
run.setOnAction(e -> { run.setOnAction(e -> {
@@ -66,33 +94,37 @@ public class CombineFilesFX extends Application {
stage.show(); stage.show();
} }
public static void combineFiles(List<File> filesToCombine) public static void combineFiles(List<File> filesToCombine) {
{
String outputPath = filesToCombine.get(0).getParent(); String outputPath = filesToCombine.get(0).getParent();
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + "reconstructed_" +filesToCombine.get(0).getName().substring( 0, (filesToCombine.get(0).getName().length() - 2)))) { try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + "reconstructed_" + filesToCombine.get(0).getName().substring(0, (filesToCombine.get(0).getName().length() - 2)))) {
System.out.println("Writing to " + outputPath + "/" + "reconstructed_" +filesToCombine.get(0).getName().substring( 0, ( filesToCombine.get(0).getName().length() - 2))); System.out.println("Writing to " + outputPath + "/" + "reconstructed_" + filesToCombine.get(0).getName().substring(0, (filesToCombine.get(0).getName().length() - 2)));
for (File file : filesToCombine) { for (File file : filesToCombine) {
try (FileInputStream dataIn = new FileInputStream(file)) { try (FileInputStream dataIn = new FileInputStream(file)) {
System.out.println("Opening the source file!"); System.out.println("Opening the source file " + file.getName() + "!");
byte[] buffer = new byte[4096]; // You can adjust the buffer size as needed byte[] buffer = new byte[4096];
int bytesRead = -1;
int bytesRead; while ((bytesRead = dataIn.read(buffer)) != -1) {
while ((bytesRead = dataIn.read(buffer)) != -1) { dataOut.write(buffer, 0, bytesRead);
dataOut.write(buffer, 0, bytesRead); }
} }
} catch (IOException ex) {
System.out.println(ex);
} }
} catch (IOException ex) { }
System.out.println(ex); } catch (IOException ex) {
} System.out.println(ex);
} }
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Success!");
alert.setContentText("Successfully combined the files!");
alert.showAndWait();
}
public static void main(String[] args) public static void main(String[] args) {
{
launch(); launch();
} }
} }
/*
*/

View File

@@ -0,0 +1,147 @@
/*
* 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 com.calebfontenot.mp5.files_calebfontenot;
import static com.calebfontenot.mp5.files_calebfontenot.SplitFilesFX.alert;
import static com.calebfontenot.mp5.files_calebfontenot.SplitFilesFX.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/**
*
* @author caleb
*/
public class HexEditorFX extends Application {
final FileChooser fileChooser = new FileChooser();
static File file = null;
@Override
public void start(Stage stage) throws Exception {
Label label = new Label("Open a file...");
Button openFileButton = new Button("Choose...");
TextArea editorWindow = new TextArea();
Button saveFileButton = new Button("Save");
editorWindow.setWrapText(true);
BorderPane bp = new BorderPane();
GridPane openFileGridPane = new GridPane();
openFileGridPane.add(label, 0, 0);
openFileGridPane.add(openFileButton, 1, 0);
bp.setTop(openFileGridPane);
bp.setCenter(editorWindow);
bp.setBottom(saveFileButton);
bp.setAlignment(saveFileButton, Pos.CENTER);
openFileButton.setOnAction(e -> {
file = fileChooser.showOpenDialog(openFileButton.getScene().getWindow());
byte[] data = null;
System.out.println("Getting data from fileand encoding it as hex...");
try (FileInputStream dataIn = new FileInputStream(file)) {
data = dataIn.readAllBytes();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
String dataString = encodeHexString(data);
editorWindow.setText(dataString);
});
saveFileButton.setOnAction(e -> {
if (file == null) {
alert.setTitle("No file selected!");
alert.setHeaderText("No file selected!");
alert.setContentText("You have to open a file first, silly!");
alert.showAndWait();
}
System.out.println("Re-encoding hex back into a byte array...");
try (FileOutputStream dataOut = new FileOutputStream(file)) {
byte[] bytesToSave = decodeHexString(editorWindow.getText());
dataOut.write(bytesToSave);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (IllegalArgumentException ex) {
alert.setTitle("Invalid Hex!");
alert.setContentText("Invalid hex entered into the text box, unable to save.");
alert.showAndWait();
}
alert.setTitle("Save successful!");
alert.setContentText("File saved successfully.");
alert.showAndWait();
});
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
private int toDigit(char hexChar) {
int digit = Character.digit(hexChar, 16);
if (digit == -1) {
throw new IllegalArgumentException(
"Invalid Hexadecimal Character: " + hexChar);
}
return digit;
}
public byte hexToByte(String hexString) {
int firstDigit = toDigit(hexString.charAt(0));
int secondDigit = toDigit(hexString.charAt(1));
return (byte) ((firstDigit << 4) + secondDigit);
}
public byte[] decodeHexString(String hexString) {
if (hexString.length() % 2 == 1) {
throw new IllegalArgumentException(
"Invalid hexadecimal String supplied.");
}
byte[] bytes = new byte[hexString.length() / 2];
for (int i = 0; i < hexString.length(); i += 2) {
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
}
return bytes;
}
public String byteToHex(byte num) {
char[] hexDigits = new char[2];
hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
hexDigits[1] = Character.forDigit((num & 0xF), 16);
return new String(hexDigits);
}
public String encodeHexString(byte[] byteArray) {
StringBuffer hexStringBuffer = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
hexStringBuffer.append(byteToHex(byteArray[i]));
}
return hexStringBuffer.toString();
}
public static void main(String[] args) {
launch(args);
}
}

View File

@@ -12,8 +12,10 @@ import java.io.IOException;
import javafx.application.Application; import javafx.application.Application;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
@@ -27,12 +29,17 @@ import javafx.stage.Stage;
public class SplitFilesFX extends Application { public class SplitFilesFX extends Application {
static File file = null; static File file = null;
final FileChooser fileChooser = new FileChooser(); final FileChooser fileChooser = new FileChooser();
static ProgressBar pb = new ProgressBar();
static Alert alert = new Alert(Alert.AlertType.INFORMATION);
@Override @Override
public void start(final Stage stage) throws Exception public void start(final Stage stage) throws Exception
{ {
fileChooser.setTitle("Open File to Split..."); fileChooser.setTitle("Open File to Split...");
BorderPane primaryBorderPane = new BorderPane(); BorderPane primaryBorderPane = new BorderPane();
BorderPane secondaryBoarderPane = new BorderPane();
GridPane textFieldGridPane = new GridPane(); GridPane textFieldGridPane = new GridPane();
pb.prefWidthProperty().bind(stage.widthProperty());
Label infoLabel = new Label("If you split a file named tmp into 3 smaller files,\n the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3. "); Label infoLabel = new Label("If you split a file named tmp into 3 smaller files,\n the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3. ");
Label chooseFile = new Label("Choose a file to split: "); Label chooseFile = new Label("Choose a file to split: ");
@@ -44,11 +51,15 @@ public class SplitFilesFX extends Application {
textFieldGridPane.add(openFilePicker, 1, 0); textFieldGridPane.add(openFilePicker, 1, 0);
textFieldGridPane.add(splitCountLabel, 0, 1); textFieldGridPane.add(splitCountLabel, 0, 1);
textFieldGridPane.add(splitCount, 1, 1); textFieldGridPane.add(splitCount, 1, 1);
primaryBorderPane.setAlignment(run, Pos.CENTER); secondaryBoarderPane.setAlignment(run, Pos.CENTER);
primaryBorderPane.setTop(infoLabel); primaryBorderPane.setTop(infoLabel);
primaryBorderPane.setCenter(textFieldGridPane); primaryBorderPane.setCenter(textFieldGridPane);
primaryBorderPane.setBottom(run);
secondaryBoarderPane.setBottom(run);
secondaryBoarderPane.setTop(pb);
primaryBorderPane.setBottom(secondaryBoarderPane);
openFilePicker.setOnAction(e -> { openFilePicker.setOnAction(e -> {
file = fileChooser.showOpenDialog(openFilePicker.getScene().getWindow()); file = fileChooser.showOpenDialog(openFilePicker.getScene().getWindow());
@@ -68,12 +79,26 @@ public class SplitFilesFX extends Application {
public static void splitFile(File fileToSplit, int splitCount) { public static void splitFile(File fileToSplit, int splitCount) {
int outputFileSize = (int) fileToSplit.length() / splitCount; int outputFileSize = (int) fileToSplit.length() / splitCount;
System.out.println("output file size will be: " + outputFileSize);
if (outputFileSize < 0) {
alert.setTitle("Output files too large");
alert.setHeaderText("Output files too large");
alert.setContentText("Please increase the amount of files to split!");
alert.showAndWait();
return;
}
double progress = 0;
String outputPath = fileToSplit.getParent(); String outputPath = fileToSplit.getParent();
// Open the original file // Open the original file
try (FileInputStream dataIn = new FileInputStream(fileToSplit)) { try (FileInputStream dataIn = new FileInputStream(fileToSplit)) {
System.out.println("Opening the source file!"); System.out.println("Opening the source file!");
for (int i = 0; i < splitCount; ++i) { for (int i = 0; i < splitCount; ++i) {
//dataIn.mark(outputFileSize * (i + 1)); progress = (i * 100) / splitCount;
System.out.println(progress);
pb.setProgress(progress);
dataIn.mark(outputFileSize * (i + 1));
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + fileToSplit.getName() + "." + (i + 1))) { try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + fileToSplit.getName() + "." + (i + 1))) {
dataOut.write(dataIn.readNBytes(outputFileSize), 0, outputFileSize); dataOut.write(dataIn.readNBytes(outputFileSize), 0, outputFileSize);
System.out.println("Writing to " + outputPath + "/" + fileToSplit.getName() + "." + (i + 1)); System.out.println("Writing to " + outputPath + "/" + fileToSplit.getName() + "." + (i + 1));

View File

@@ -0,0 +1,944 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>PriorityQueueASDV.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #9999ff; font-family: monospace; font-style: italic}
.ST5 {font-family: monospace; font-weight: bold; font-style: italic}
.string {color: #6a8759}
.number {color: #6897bb}
.ST1 {color: #9876aa}
.ST4 {color: #ffc66d}
.ST3 {color: #8a653b}
.ST6 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST7 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP6_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp6_calebfontenot/PriorityQueueASDV.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> edu.slcc.asdv.caleb.mp6_calebfontenot;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.Arrays;
<span class="literal">import</span> java.util.Collection;
<span class="literal">import</span> java.util.Iterator;
<span class="literal">import</span> java.util.List;
<span class="literal">import</span> java.util.NoSuchElementException;
<span class="literal">import</span> java.util.PriorityQueue;
<span class="literal">import</span> java.util.Queue;
<span class="literal">import</span> java.util.function.Consumer;
<span class="literal">public</span> <span class="literal">class</span> PriorityQueueASDV&lt;E <span class="literal">extends</span> Comparable&lt;E&gt;&gt;
<span class="literal">implements</span> Queue&lt;E&gt;, Cloneable {
<span class="literal">private</span> Node&lt;E&gt; <span class="ST1">head</span>;<span class="comment">//head</span>
<span class="literal">private</span> Node&lt;E&gt; <span class="ST1">tail</span>;<span class="comment">//tail</span>
<span class="literal">class</span> Node&lt;E&gt; {
E <span class="ST1">e</span>;
Node&lt;E&gt; <span class="ST1">l</span>;
Node&lt;E&gt; <span class="ST1">r</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Inserts</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">into</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">if</span> <span class="comment">it</span> <span class="comment">is</span> <span class="comment">possible</span> <span class="comment">to</span> <span class="comment">do</span> <span class="comment">so</span> <span class="comment">immediately</span> <span class="comment">without</span> <span class="comment">violating</span> <span class="comment">capacity</span> <span class="comment">restrictions</span><span class="comment">, </span><span class="comment">returning</span> <span class="comment">true</span> <span class="comment">upon</span> <span class="comment">success</span> <span class="comment">and</span> <span class="comment">throwing</span> <span class="comment">an</span> <span class="comment">IllegalStateException</span> <span class="comment">if</span> <span class="comment">no</span> <span class="comment">space</span> <span class="comment">is</span> <span class="comment">currently</span> <span class="comment">available</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">Specified</span> <span class="comment">by</span><span class="comment">: </span><span class="comment">add</span> <span class="comment">in</span> <span class="comment">interface</span> <span class="comment">Collection</span><span class="ST2">&lt;E&gt;</span>
<span class="comment"> * </span><span class="comment">Parameters</span><span class="comment">: </span><span class="comment">e</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">element</span> <span class="comment">to</span> <span class="comment">add</span> <span class="comment">Returns</span><span class="comment">: </span><span class="comment">true</span><span class="comment"> (</span><span class="comment">as</span> <span class="comment">specified</span> <span class="comment">by</span> <span class="comment">Collection</span><span class="comment">.</span><span class="comment">add</span><span class="comment">(</span><span class="comment">E</span><span class="comment">)) </span><span class="comment">Throws</span><span class="comment">: </span><span class="comment">IllegalStateException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">element</span> <span class="comment">cannot</span> <span class="comment">be</span> <span class="comment">added</span> <span class="comment">at</span> <span class="comment">this</span> <span class="comment">time</span> <span class="comment">due</span> <span class="comment">to</span> <span class="comment">capacity</span> <span class="comment">restrictions</span> <span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">class</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span> <span class="comment">IllegalArgumentException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">some</span> <span class="comment">property</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">element</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">queue</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">e</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">element</span> <span class="comment">to</span> <span class="comment">add</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">changed</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">result</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">call</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalStateException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">element</span> <span class="comment">cannot</span> <span class="comment">be</span> <span class="comment">added</span> <span class="comment">at</span> <span class="comment">this</span> <span class="comment">time</span> <span class="comment">due</span> <span class="comment">to</span> <span class="comment">capacity</span> <span class="comment">restrictions</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">class</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalArgumentException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">some</span> <span class="comment">property</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">element</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">queue</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">add</span>(E e) {
<span class="literal">if</span> (e == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">NULL elements not allowed!</span><span class="string">&quot;</span>);
}
Node&lt;E&gt; newNode = <span class="literal">new</span> Node&lt;E&gt;();
newNode.<span class="ST1">e</span> = e;
<span class="comment">//1. empty queue</span>
<span class="literal">if</span> (<span class="literal">this</span>.<span class="ST1">head</span> == <span class="literal">null</span> &amp;&amp; <span class="literal">this</span>.<span class="ST1">tail</span> == <span class="literal">null</span>) {
<span class="literal">this</span>.<span class="ST1">head</span> = <span class="literal">this</span>.<span class="ST1">tail</span> = newNode;
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">int</span> index = findCorrectPositionToInsertElement(e);
<span class="comment">//int index = findCorrectPositionToInsertElementHashCode(e);</span>
<span class="comment">//2. we add at size ( last node)</span>
<span class="literal">if</span> (index == size()) {
<span class="ST1">tail</span>.<span class="ST1">r</span> = newNode;<span class="comment">//1</span>
newNode.<span class="ST1">l</span> = <span class="ST1">tail</span>;<span class="comment">//2</span>
<span class="ST1">tail</span> = newNode;<span class="comment">//3</span>
} <span class="comment">//3. we add at 0 in the front</span>
<span class="literal">else</span> <span class="literal">if</span> (index == <span class="number">0</span>) {
newNode.<span class="ST1">r</span> = <span class="ST1">head</span>;
<span class="literal">this</span>.<span class="ST1">head</span>.<span class="ST1">l</span> = newNode;
<span class="literal">this</span>.<span class="ST1">head</span> = newNode;
<span class="literal">if</span> (size() == <span class="number">1</span>) {
<span class="ST1">tail</span> = <span class="ST1">head</span>;
}
} <span class="comment">//4. we add in the middle</span>
<span class="literal">else</span> {
Node&lt;E&gt; p = <span class="ST1">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; index - <span class="number">1</span>; ++i) {
p = p.<span class="ST1">r</span>;
}
<span class="comment">//after for loop p point one position before insertion</span>
newNode.<span class="ST1">l</span> = p;<span class="comment">//we connect the left of the new node </span>
<span class="comment">//to the node that is BEFORE </span>
<span class="comment">//the node to be inserted</span>
newNode.<span class="ST1">r</span> = p.<span class="ST1">r</span>;<span class="comment">//we connect the right of the new node</span>
<span class="comment">// to the node thta is AFTER </span>
<span class="comment">//the node to be inserted</span>
p.<span class="ST1">r</span> = newNode; <span class="comment">//we connect the right the node BEFORE the node</span>
<span class="comment">//to be inserted to the new node</span>
p.<span class="ST1">r</span>.<span class="ST1">r</span>.<span class="ST1">l</span> = newNode;<span class="comment">//we connect the left of the node AFTER the node </span>
<span class="comment">//to be iserted to the new node</span>
}
<span class="literal">return</span> <span class="literal">true</span>;
}
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST4">size</span>() {
Node&lt;E&gt; p = <span class="literal">this</span>.<span class="ST1">head</span>;
<span class="literal">int</span> count = <span class="number">0</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
p = p.<span class="ST1">r</span>;
count++;
}
<span class="literal">return</span> count;
}
<span class="literal">private</span> <span class="literal">int</span> <span class="ST4">findCorrectPositionToInsertElement</span>(E e) {
Node&lt;E&gt; p = <span class="literal">this</span>.<span class="ST1">head</span>;
<span class="literal">int</span> pos = <span class="number">0</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
<span class="literal">if</span> (e.compareTo(p.<span class="ST1">e</span>) &gt; <span class="number">0</span>) {
p = p.<span class="ST1">r</span>;
++pos;
} <span class="literal">else</span> {
<span class="literal">break</span>;
}
}
<span class="literal">return</span> pos;
}
<span class="literal">private</span> <span class="literal">int</span> <span class="comment">findCorrectPositionToInsertElementHashCode</span>(E e) {
Node&lt;E&gt; p = <span class="literal">this</span>.<span class="ST1">head</span>;
<span class="literal">int</span> pos = <span class="number">0</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
<span class="literal">if</span> (e.hashCode() &gt; p.<span class="ST1">e</span>.hashCode()) {
p = p.<span class="ST1">r</span>;
++pos;
} <span class="literal">else</span> {
<span class="literal">break</span>;
}
}
<span class="literal">return</span> pos;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Inserts</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">into</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">if</span> <span class="comment">it</span> <span class="comment">is</span> <span class="comment">possible</span> <span class="comment">to</span> <span class="comment">do</span> <span class="comment">so</span> <span class="comment">immediately</span> <span class="comment">without</span> <span class="comment">violating</span> <span class="comment">capacity</span> <span class="comment">restrictions</span><span class="comment">.</span> <span class="comment">When</span> <span class="comment">using</span> <span class="comment">a</span> <span class="comment">capacity</span><span class="comment">-</span><span class="comment">restricted</span> <span class="comment">queue</span><span class="comment">, </span><span class="comment">this</span> <span class="comment">method</span> <span class="comment">is</span> <span class="comment">generally</span> <span class="comment">preferable</span> <span class="comment">to</span> <span class="comment">add</span><span class="comment">(</span><span class="comment">E</span><span class="comment">), </span><span class="comment">which</span> <span class="comment">can</span> <span class="comment">fail</span> <span class="comment">to</span> <span class="comment">insert</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">only</span> <span class="comment">by</span> <span class="comment">throwing</span> <span class="comment">an</span> <span class="comment">exception</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">e</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">element</span> <span class="comment">to</span> <span class="comment">add</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalStateException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">element</span> <span class="comment">cannot</span> <span class="comment">be</span> <span class="comment">added</span> <span class="comment">at</span> <span class="comment">this</span> <span class="comment">time</span> <span class="comment">due</span> <span class="comment">to</span> <span class="comment">capacity</span> <span class="comment">restrictions</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">class</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalArgumentException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">some</span> <span class="comment">property</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">element</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">queue</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">element</span> <span class="comment">was</span> <span class="comment">added</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">offer</span>(E e) {
<span class="literal">return</span> add(e);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Retrieves</span> <span class="comment">and</span> <span class="comment">removes</span> <span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">.</span> <span class="comment">This</span> <span class="comment">method</span> <span class="comment">differs</span> <span class="comment">from</span><span class="comment"> {</span><span class="comment">@link</span> <span class="comment">#</span><span class="comment">poll</span> <span class="comment">poll</span><span class="comment">} </span><span class="comment">only</span> <span class="comment">in</span> <span class="comment">that</span> <span class="comment">it</span> <span class="comment">throws</span> <span class="comment">an</span> <span class="comment">exception</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">This</span> <span class="comment">implementation</span> <span class="comment">returns</span> <span class="comment">the</span> <span class="comment">result</span> <span class="comment">of</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">poll</span><span class="comment">}</span> <span class="comment">unless</span> <span class="comment">the</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NoSuchElementException</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> E <span class="ST4">remove</span>() {
Node&lt;E&gt; <span class="comment">pointer</span> = <span class="ST1">head</span>.<span class="ST1">r</span>;
E removedElement = <span class="ST1">head</span>.<span class="ST1">e</span>;
<span class="ST1">head</span> = <span class="ST1">head</span>.<span class="ST1">r</span>;
<span class="ST1">head</span>.<span class="ST1">l</span> = <span class="literal">null</span>;
<span class="literal">return</span> removedElement;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Retrieves</span> <span class="comment">and</span> <span class="comment">removes</span> <span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">returns</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">Returns</span><span class="comment">: </span><span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> E <span class="ST4">poll</span>() {
<span class="literal">if</span> (size() == <span class="number">0</span>) {
<span class="literal">return</span> <span class="literal">null</span>;
}
<span class="literal">if</span> (size() &gt; <span class="number">1</span>) {
<span class="ST1">head</span> = <span class="ST1">head</span>.<span class="ST1">r</span>;
E e = <span class="ST1">head</span>.<span class="ST1">l</span>.<span class="ST1">e</span>;
<span class="ST1">head</span>.<span class="ST1">l</span> = <span class="literal">null</span>;
<span class="literal">return</span> e;
} <span class="literal">else</span> <span class="comment">//size 1</span>
{
E e = <span class="ST1">head</span>.<span class="ST1">e</span>;
<span class="ST1">head</span> = <span class="ST1">tail</span> = <span class="literal">null</span>;
<span class="literal">return</span> e;
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Retrieves</span><span class="comment">, </span><span class="comment">but</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">remove</span><span class="comment">, </span><span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">.</span> <span class="comment">This</span> <span class="comment">method</span> <span class="comment">differs</span> <span class="comment">from</span><span class="comment"> {</span><span class="comment">@link</span> <span class="comment">#</span><span class="comment">peek</span> <span class="comment">peek</span><span class="comment">} </span><span class="comment">only</span> <span class="comment">in</span> <span class="comment">that</span> <span class="comment">it</span> <span class="comment">throws</span> <span class="comment">an</span> <span class="comment">exception</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NoSuchElementException</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> E <span class="ST4">element</span>() {
<span class="literal">if</span> (<span class="ST1">head</span> != <span class="literal">null</span>) {
<span class="literal">return</span> (E) <span class="ST1">head</span>;
} <span class="literal">else</span> {
<span class="literal">throw</span> <span class="literal">new</span> NoSuchElementException(<span class="string">&quot;</span><span class="string">Element does not exist.</span><span class="string">&quot;</span>);
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Retrieves</span><span class="comment">, </span><span class="comment">but</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">remove</span><span class="comment">, </span><span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">returns</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">null</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">, </span><span class="comment">or</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">null</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> E <span class="ST4">peek</span>() {
<span class="literal">return</span> (E) <span class="ST1">head</span>;
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">isEmpty</span>() {
<span class="literal">return</span> <span class="ST1">head</span> == <span class="literal">null</span> &amp;&amp; <span class="ST1">tail</span> == <span class="literal">null</span> ? <span class="literal">true</span> : <span class="literal">false</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">true</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span><span class="comment">.</span> <span class="comment">More</span> <span class="comment">formally</span><span class="comment">, </span><span class="comment">returns</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">true</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">and</span> <span class="comment">only</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">at</span> <span class="comment">least</span> <span class="comment">one</span> <span class="comment">element</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">e</span><span class="comment">}</span> <span class="comment">such</span> <span class="comment">that</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">Objects</span><span class="comment">.</span><span class="comment">equals</span><span class="comment">(</span><span class="comment">o</span><span class="comment">, </span><span class="comment">e</span><span class="comment">)</span><span class="comment">}</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">o</span> <span class="comment">element</span> <span class="comment">whose</span> <span class="comment">presence</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">tested</span>
<span class="comment"> * </span><span class="comment">@return</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">true</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">incompatible</span> <span class="comment">with</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment"> ({</span><span class="comment">@linkplain</span> <span class="comment">Collection</span><span class="comment">#</span><span class="comment">#</span><span class="comment">optional</span><span class="comment">-</span><span class="comment">restrictions</span> <span class="comment">optional</span><span class="comment">})</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span><span class="comment"> ({</span><span class="comment">@linkplain</span> <span class="comment">Collection</span><span class="comment">#</span><span class="comment">#</span><span class="comment">optional</span><span class="comment">-</span><span class="comment">restrictions</span> <span class="comment">optional</span><span class="comment">})</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">contains</span>(Object o) {
Node&lt;E&gt; pointer = <span class="ST1">head</span>;
<span class="literal">do</span> {
<span class="literal">if</span> (pointer.equals(o)) {
<span class="literal">return</span> <span class="literal">true</span>;
} <span class="literal">else</span> {
pointer = pointer.<span class="ST1">r</span>;
}
} <span class="literal">while</span> (pointer != <span class="literal">null</span>);
<span class="literal">return</span> <span class="literal">false</span>;
}
@Override
<span class="literal">public</span> Iterator&lt;E&gt; <span class="ST4">iterator</span>() {
Iterator&lt;E&gt; it = <span class="literal">new</span> Iterator&lt;E&gt;() {
Node&lt;E&gt; <span class="ST1">p</span> = <span class="ST1">head</span>;
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">hasNext</span>() {
<span class="literal">return</span> <span class="ST1">p</span> == <span class="literal">null</span> ? <span class="literal">false</span> : <span class="literal">true</span>;
}
@Override
<span class="literal">public</span> E <span class="ST4">next</span>() {
<span class="literal">if</span> (hasNext() == <span class="literal">false</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NoSuchElementException(<span class="string">&quot;</span><span class="string">the is no next element</span><span class="string">&quot;</span>);
}
E e = <span class="ST1">p</span>.<span class="ST1">e</span>;
<span class="ST1">p</span> = <span class="ST1">p</span>.<span class="ST1">r</span>;
<span class="literal">return</span> e;
}
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">forEachRemaining</span>(Consumer&lt;? <span class="literal">super</span> E&gt; action) {
<span class="literal">while</span> (hasNext()) {
action.accept(<span class="ST1">p</span>.<span class="ST1">e</span>);
<span class="ST1">p</span> = <span class="ST1">p</span>.<span class="ST1">r</span>;
}
}
};
<span class="literal">return</span> it;
}
@Override
<span class="literal">public</span> Object[] <span class="ST4">toArray</span>() {
Node&lt;E&gt; pointer = <span class="ST1">head</span>;
Object[] returnArray = <span class="literal">new</span> Object[<span class="literal">this</span>.size()];
<span class="literal">int</span> i = <span class="number">0</span>;
<span class="literal">while</span> (pointer.<span class="ST1">r</span> != <span class="literal">null</span>) {
returnArray[i++] = pointer.<span class="ST1">e</span>;
pointer = pointer.<span class="ST1">r</span>;
}
returnArray[i++] = pointer.<span class="ST1">e</span>;
<span class="literal">return</span> returnArray;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">an</span> <span class="comment">array</span> <span class="comment">containing</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">; </span><span class="comment">the</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">returned</span> <span class="comment">array</span> <span class="comment">is</span> <span class="comment">that</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">collection</span> <span class="comment">fits</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span><span class="comment">, </span><span class="comment">it</span> <span class="comment">is</span> <span class="comment">returned</span> <span class="comment">therein</span><span class="comment">.</span> <span class="comment">Otherwise</span><span class="comment">, </span><span class="comment">a</span> <span class="comment">new</span> <span class="comment">array</span> <span class="comment">is</span> <span class="comment">allocated</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span> <span class="comment">and</span> <span class="comment">the</span> <span class="comment">size</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">If</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">fits</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span> <span class="comment">with</span> <span class="comment">room</span> <span class="comment">to</span> <span class="comment">spare</span><span class="comment"> (</span><span class="comment">i</span><span class="comment">.</span><span class="comment">e</span><span class="comment">.</span><span class="comment">, </span><span class="comment">the</span> <span class="comment">array</span> <span class="comment">has</span> <span class="comment">more</span> <span class="comment">elements</span> <span class="comment">than</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">), </span><span class="comment">the</span> <span class="comment">element</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">immediately</span> <span class="comment">following</span> <span class="comment">the</span> <span class="comment">end</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">set</span> <span class="comment">to</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">null</span><span class="comment">}</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">This</span> <span class="comment">is</span> <span class="comment">useful</span> <span class="comment">in</span> <span class="comment">determining</span> <span class="comment">the</span> <span class="comment">length</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="ST2">&lt;i&gt;</span><span class="comment">only</span><span class="ST2">&lt;/i&gt;</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">caller</span> <span class="comment">knows</span> <span class="comment">that</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">contain</span> <span class="comment">any</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">null</span><span class="comment">}</span> <span class="comment">elements</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">If</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">makes</span> <span class="comment">any</span> <span class="comment">guarantees</span> <span class="comment">as</span> <span class="comment">to</span> <span class="comment">what</span> <span class="comment">order</span> <span class="comment">its</span> <span class="comment">elements</span> <span class="comment">are</span> <span class="comment">returned</span> <span class="comment">by</span> <span class="comment">its</span> <span class="comment">iterator</span><span class="comment">, </span><span class="comment">this</span> <span class="comment">method</span> <span class="comment">must</span> <span class="comment">return</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">same</span> <span class="comment">order</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@apiNote</span> <span class="comment">This</span> <span class="comment">method</span> <span class="comment">acts</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">bridge</span> <span class="comment">between</span> <span class="comment">array</span><span class="comment">-</span><span class="comment">based</span> <span class="comment">and</span> <span class="comment">collection</span><span class="comment">-</span><span class="comment">based</span> <span class="comment">APIs</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">allows</span> <span class="comment">an</span> <span class="comment">existing</span> <span class="comment">array</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">reused</span> <span class="comment">under</span> <span class="comment">certain</span> <span class="comment">circumstances</span><span class="comment">.</span> <span class="comment">Use</span><span class="comment"> {</span><span class="comment">@link</span> <span class="comment">#</span><span class="comment">toArray</span><span class="comment">()} </span><span class="comment">to</span> <span class="comment">create</span> <span class="comment">an</span> <span class="comment">array</span> <span class="comment">whose</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">is</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">Object</span><span class="comment">[]</span><span class="comment">}, </span><span class="comment">or</span> <span class="comment">use</span><span class="comment"> {</span><span class="comment">@link</span> <span class="comment">#</span><span class="comment">toArray</span><span class="comment">(</span><span class="comment">IntFunction</span><span class="comment">)} </span><span class="comment">to</span> <span class="comment">control</span> <span class="comment">the</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">array</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">Suppose</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">x</span><span class="comment">}</span> <span class="comment">is</span> <span class="comment">a</span> <span class="comment">collection</span> <span class="comment">known</span> <span class="comment">to</span> <span class="comment">contain</span> <span class="comment">only</span> <span class="comment">strings</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">following</span> <span class="comment">code</span> <span class="comment">can</span> <span class="comment">be</span> <span class="comment">used</span> <span class="comment">to</span> <span class="comment">dump</span> <span class="comment">the</span> <span class="comment">collection</span> <span class="comment">into</span> <span class="comment">a</span> <span class="comment">previously</span> <span class="comment">allocated</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">String</span><span class="comment">}</span> <span class="comment">array</span><span class="comment">:</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;pre&gt;</span>
<span class="comment"> * </span><span class="comment">String</span><span class="comment">[] </span><span class="comment">y</span><span class="comment"> = </span><span class="comment">new</span> <span class="comment">String</span><span class="comment">[</span><span class="comment">SIZE</span><span class="comment">];</span>
<span class="comment"> * </span><span class="comment">.</span><span class="comment">.</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">y</span><span class="comment"> = </span><span class="comment">x</span><span class="comment">.</span><span class="comment">toArray</span><span class="comment">(</span><span class="comment">y</span><span class="comment">);</span><span class="ST2">&lt;/pre&gt;</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">The</span> <span class="comment">return</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">reassigned</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">variable</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">y</span><span class="comment">}</span><span class="comment">, </span><span class="comment">because</span> <span class="comment">a</span> <span class="comment">new</span> <span class="comment">array</span> <span class="comment">will</span> <span class="comment">be</span> <span class="comment">allocated</span> <span class="comment">and</span> <span class="comment">returned</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">collection</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">x</span><span class="comment">}</span> <span class="comment">has</span> <span class="comment">too</span> <span class="comment">many</span> <span class="comment">elements</span> <span class="comment">to</span> <span class="comment">fit</span> <span class="comment">into</span> <span class="comment">the</span> <span class="comment">existing</span> <span class="comment">array</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">y</span><span class="comment">}</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">Note</span> <span class="comment">that</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">toArray</span><span class="comment">(</span><span class="comment">new</span> <span class="comment">Object</span><span class="comment">[0])</span><span class="comment">} </span><span class="comment">is</span> <span class="comment">identical</span> <span class="comment">in</span> <span class="comment">function</span> <span class="comment">to</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">toArray</span><span class="comment">()</span><span class="comment">}</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">&lt;T&gt;</span> <span class="comment">the</span> <span class="comment">component</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">to</span> <span class="comment">contain</span> <span class="comment">the</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">a</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">into</span> <span class="comment">which</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">are</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">stored</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">it</span> <span class="comment">is</span> <span class="comment">big</span> <span class="comment">enough</span><span class="comment">; </span><span class="comment">otherwise</span><span class="comment">, </span><span class="comment">a</span> <span class="comment">new</span> <span class="comment">array</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">same</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">is</span> <span class="comment">allocated</span> <span class="comment">for</span> <span class="comment">this</span> <span class="comment">purpose</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">an</span> <span class="comment">array</span> <span class="comment">containing</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ArrayStoreException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">any</span> <span class="comment">element</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">not</span> <span class="comment">assignable</span> <span class="comment">to</span> <span class="comment">the</span><span class="comment"> {</span><span class="comment">@linkplain</span> <span class="comment">Class</span><span class="comment">#</span><span class="comment">getComponentType</span>
<span class="comment"> * </span><span class="comment">runtime</span> <span class="comment">component</span> <span class="comment">type</span><span class="comment">} </span><span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span> <span class="comment">is</span> <span class="comment">null</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> &lt;T&gt; T[] <span class="ST4">toArray</span>(T[] a) {
a = Arrays.<span class="ST5">copyOf</span>(a, <span class="literal">t</span><span class="literal">his</span>.size());
Node&lt;E&gt; pointer = <span class="ST1">head</span>;
System.<span class="ST6">out</span>.println(a.getClass());
System.<span class="ST6">out</span>.println(pointer.getClass());
System.<span class="ST6">out</span>.println(pointer.<span class="ST1">e</span>.getClass());
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="literal">this</span>.size(); ++i) {
a[i] = (T) pointer.<span class="ST1">e</span>;
pointer = pointer.<span class="ST1">r</span>;
}
<span class="literal">return</span> a;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">a</span> <span class="comment">single</span> <span class="comment">instance</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">it</span> <span class="comment">is</span> <span class="comment">present</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">More</span> <span class="comment">formally</span><span class="comment">, </span><span class="comment">removes</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">e</span> <span class="comment">such</span> <span class="comment">that</span><span class="comment"> (</span><span class="comment">o</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> ? </span><span class="comment">e</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> : </span><span class="comment">o</span><span class="comment">.</span><span class="comment">equals</span><span class="comment">(</span><span class="comment">e</span><span class="comment">)), </span><span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">such</span> <span class="comment">elements</span><span class="comment">.</span> <span class="comment">Returns</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contained</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span><span class="comment"> (</span><span class="comment">or</span> <span class="comment">equivalently</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">changed</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">result</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">call</span><span class="comment">)</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">o</span><span class="comment"> - </span><span class="comment">element</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">removed</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">present</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">incompatible</span> <span class="comment">with</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">was</span> <span class="comment">removed</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">result</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">call</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">remove</span>(Object o) {
<span class="literal">if</span> (o == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">null vales not allowed</span><span class="string">&quot;</span>);
}
<span class="literal">if</span> (size() == <span class="number">0</span>) {
<span class="literal">return</span> <span class="literal">false</span>;
}
Node&lt;E&gt; p = <span class="literal">this</span>.<span class="ST1">head</span>;
<span class="literal">int</span> pos = <span class="number">0</span>;
<span class="literal">while</span> (p != <span class="literal">this</span>.<span class="ST1">tail</span>) {
<span class="literal">if</span> (p.<span class="ST1">e</span>.equals(o)) {
<span class="literal">if</span> (size() == <span class="number">1</span>) {
<span class="literal">this</span>.<span class="ST1">head</span> = <span class="literal">this</span>.<span class="ST1">tail</span> = <span class="literal">null</span>;
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">this</span>.removeAt(pos, (E) o);
<span class="literal">break</span>;
}
++pos;
p = p.<span class="ST1">r</span>;
}
<span class="literal">if</span> (p == <span class="ST1">tail</span> &amp;&amp; p.<span class="ST1">e</span>.equals(o)) {
<span class="literal">this</span>.removeAt(size() - <span class="number">1</span>, (E) o);
}
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">pos</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">e</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IndexOutOfBoundsException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">pos</span> <span class="comment">less</span><span class="comment"> 0 </span><span class="comment">OR</span> <span class="comment">pos</span> <span class="comment">greater</span><span class="comment">-</span><span class="comment">equal</span> <span class="comment">size</span><span class="comment">()</span>
<span class="comment"> * </span><span class="comment">@return</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">boolean</span> <span class="ST4">removeAt</span>(<span class="literal">int</span> pos, E <span class="comment">e</span>) {
<span class="literal">if</span> (pos &lt; <span class="number">0</span> || pos &gt;= size()) {
<span class="literal">throw</span> <span class="literal">new</span> IndexOutOfBoundsException(pos + <span class="string">&quot;</span><span class="string"> is out of bounds</span><span class="string">&quot;</span>);
}
<span class="comment">//1.list is empty</span>
<span class="literal">if</span> (isEmpty()) {
<span class="literal">return</span> <span class="literal">false</span>;
} <span class="comment">//2. one node exists</span>
<span class="literal">else</span> <span class="literal">if</span> (size() == <span class="number">1</span>) {
<span class="literal">this</span>.<span class="ST1">head</span> = <span class="literal">this</span>.<span class="ST1">tail</span> = <span class="literal">null</span>;
} <span class="comment">//3. remove in the front( head)</span>
<span class="literal">else</span> <span class="literal">if</span> (pos == <span class="number">0</span>) {
<span class="literal">this</span>.<span class="ST1">head</span> = <span class="literal">this</span>.<span class="ST1">head</span>.<span class="ST1">r</span>;
<span class="ST1">head</span>.<span class="ST1">l</span> = <span class="literal">null</span>;
} <span class="comment">//4. remove in the end ( tail)</span>
<span class="literal">else</span> <span class="literal">if</span> (pos == size() - <span class="number">1</span>) {
<span class="literal">this</span>.<span class="ST1">tail</span> = <span class="literal">this</span>.<span class="ST1">tail</span>.<span class="ST1">l</span>;
<span class="literal">this</span>.<span class="ST1">tail</span>.<span class="ST1">r</span> = <span class="literal">null</span>;
} <span class="comment">//5. remove in the middle ( at least 3 nodes are in the queue)</span>
<span class="literal">else</span> {
Node&lt;E&gt; p = <span class="ST1">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; pos - <span class="number">1</span>; ++i) {
p = p.<span class="ST1">r</span>;
}
p.<span class="ST1">r</span> = p.<span class="ST1">r</span>.<span class="ST1">r</span>;
p.<span class="ST1">r</span>.<span class="ST1">l</span> = p;
}
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">true</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">c</span> <span class="comment">collection</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">checked</span> <span class="comment">for</span> <span class="comment">containment</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@return</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">true</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">types</span> <span class="comment">of</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">are</span> <span class="comment">incompatible</span> <span class="comment">with</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment"> ({</span><span class="comment">@linkplain</span> <span class="comment">Collection</span><span class="comment">#</span><span class="comment">#</span><span class="comment">optional</span><span class="comment">-</span><span class="comment">restrictions</span> <span class="comment">optional</span><span class="comment">})</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">null</span> <span class="comment">elements</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span><span class="comment"> ({</span><span class="comment">@linkplain</span> <span class="comment">Collection</span><span class="comment">#</span><span class="comment">#</span><span class="comment">optional</span><span class="comment">-</span><span class="comment">restrictions</span> <span class="comment">optional</span><span class="comment">}) </span><span class="comment">or</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">null</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@see</span> <span class="comment">#</span><span class="comment">contains</span><span class="comment">(</span><span class="comment">Object</span><span class="comment">)</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">containsAll</span>(Collection&lt;?&gt; c) {
<span class="comment">// Java already throws a CastCastException if you give it the wrong type, so we don&#39;t have to throw that ourselves</span>
<span class="literal">if</span> (c.contains(<span class="literal">n</span><span class="literal">ull</span>) || c == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">The collection you passed to containsAll() contains a null element. Cannot continue.</span><span class="string">&quot;</span>);
}
<span class="comment">// Unpack the collection so we can compare them</span>
Object[] compareArray = c.toArray();
Node&lt;E&gt; pointer = <span class="literal">null</span>;
<span class="literal">int</span> matchCount = <span class="number">0</span>;
<span class="literal">for</span> (Object compare : compareArray) {
pointer = <span class="ST1">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; size() - <span class="number">1</span>; ++i) {
<span class="literal">if</span> (pointer.<span class="ST1">e</span>.equals(compare)) {
matchCount++;
}
pointer = pointer.<span class="ST1">r</span>;
}
}
<span class="literal">if</span> (matchCount == compareArray.<span class="ST1">length</span> - <span class="number">1</span>) {
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Adds</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">behavior</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">operation</span> <span class="comment">is</span> <span class="comment">undefined</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">modified</span> <span class="comment">while</span> <span class="comment">the</span> <span class="comment">operation</span> <span class="comment">is</span> <span class="comment">in</span> <span class="comment">progress</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">This</span> <span class="comment">implies</span> <span class="comment">that</span> <span class="comment">the</span> <span class="comment">behavior</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">call</span> <span class="comment">is</span> <span class="comment">undefined</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">nonempty</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">c</span><span class="comment"> - </span><span class="comment">collection</span> <span class="comment">containing</span> <span class="comment">elements</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">class</span> <span class="comment">of</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">a</span> <span class="comment">null</span> <span class="comment">element</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">null</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalArgumentException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">some</span> <span class="comment">property</span> <span class="comment">of</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalArgumentException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">some</span> <span class="comment">property</span> <span class="comment">of</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">changed</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">result</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">call</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">addAll</span>(Collection&lt;? <span class="literal">extends</span> E&gt; c) {
<span class="literal">int</span> sizeBefore = size();
<span class="literal">for</span> (E e : c) {
add(e);
}
<span class="literal">int</span> sizeAfter = size();
<span class="literal">return</span> sizeAfter &gt; sizeBefore;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">elements</span> <span class="comment">that</span> <span class="comment">are</span> <span class="comment">also</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">After</span> <span class="comment">this</span> <span class="comment">call</span> <span class="comment">returns</span><span class="comment">, </span><span class="comment">this</span> <span class="comment">collection</span> <span class="comment">will</span> <span class="comment">contain</span> <span class="comment">no</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">common</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@implSpec</span> <span class="comment">This</span> <span class="comment">implementation</span> <span class="comment">iterates</span> <span class="comment">over</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">, </span><span class="comment">checking</span> <span class="comment">each</span> <span class="comment">element</span> <span class="comment">returned</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">iterator</span> <span class="comment">in</span> <span class="comment">turn</span> <span class="comment">to</span> <span class="comment">see</span> <span class="comment">if</span> <span class="comment">it</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">it</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">so</span> <span class="comment">contained</span><span class="comment">, </span><span class="comment">it</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">removed</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">iterator</span><span class="comment">&#39;</span><span class="comment">s</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">remove</span><span class="comment">}</span> <span class="comment">method</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">Note</span> <span class="comment">that</span> <span class="comment">this</span> <span class="comment">implementation</span> <span class="comment">will</span> <span class="comment">throw</span> <span class="comment">an</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">UnsupportedOperationException</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">iterator</span> <span class="comment">returned</span> <span class="comment">by</span> <span class="comment">the</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">iterator</span><span class="comment">}</span> <span class="comment">method</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">implement</span> <span class="comment">the</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">remove</span><span class="comment">}</span> <span class="comment">method</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">common</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">UnsupportedOperationException</span><span class="comment"> {</span><span class="comment">@inheritDoc</span><span class="comment">}</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span><span class="comment"> {</span><span class="comment">@inheritDoc</span><span class="comment">}</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> {</span><span class="comment">@inheritDoc</span><span class="comment">}</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@see</span> <span class="comment">#</span><span class="comment">remove</span><span class="comment">(</span><span class="comment">Object</span><span class="comment">)</span>
<span class="comment"> * </span><span class="comment">@see</span> <span class="comment">#</span><span class="comment">contains</span><span class="comment">(</span><span class="comment">Object</span><span class="comment">)</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">removeAll</span>(Collection&lt;?&gt; c) {
<span class="literal">if</span> (c.contains(<span class="literal">n</span><span class="literal">ull</span>) || c == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">The collection you passed to removeAll() contains a null element. Cannot continue.</span><span class="string">&quot;</span>);
}
<span class="comment">// Unpack the collection so we can remove them</span>
Object[] compareArray = c.toArray();
Node&lt;E&gt; pointer = <span class="literal">null</span>;
<span class="literal">boolean</span> removeSuccessful = <span class="literal">false</span>;
<span class="literal">for</span> (Object compare : compareArray) {
pointer = <span class="ST1">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; size() - <span class="number">1</span>; ++i) {
<span class="literal">if</span> (pointer.<span class="ST1">e</span>.equals(compare)) {
remove(pointer.<span class="ST1">e</span>);
removeSuccessful = <span class="literal">true</span>;
}
pointer = pointer.<span class="ST1">r</span>;
}
}
<span class="literal">return</span> removeSuccessful;
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">retainAll</span>(Collection&lt;?&gt; c) {
<span class="literal">if</span> (c.contains(<span class="literal">n</span><span class="literal">ull</span>) || c == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">The collection you passed to retainAll() contains a null element. Cannot continue.</span><span class="string">&quot;</span>);
}
Node&lt;E&gt; pointer = <span class="literal">null</span>;
<span class="literal">boolean</span> removeSuccessful = <span class="literal">false</span>;
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; c.size() - <span class="number">1</span>; ++j) {
pointer = <span class="ST1">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; size(); ++i) {
<span class="literal">if</span> (!c.contains(pointer.<span class="ST1">e</span>)) {
remove(pointer.<span class="ST1">e</span>);
removeSuccessful = <span class="literal">true</span>;
}
pointer = pointer.<span class="ST1">r</span>;
}
}
<span class="literal">return</span> removeSuccessful;
}
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">clear</span>() {
<span class="comment">// head = tail = null;</span>
<span class="comment">//extra, no necessary to set the link of every node</span>
Node&lt;E&gt; p = <span class="ST1">head</span>;
<span class="literal">while</span> (p != <span class="ST1">tail</span>) {
p = p.<span class="ST1">r</span>;
p.<span class="ST1">l</span> = <span class="literal">null</span>;
}
<span class="ST1">head</span> = <span class="ST1">tail</span> = <span class="literal">null</span>;
}
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">forEach</span>(Consumer&lt;? <span class="literal">super</span> E&gt; action) {
<span class="comment">//1. use a pointer that points to the head</span>
<span class="comment">//2. while the pointer has not reached the end of the queue </span>
<span class="comment">//consume it</span>
Node&lt;E&gt; p = <span class="ST1">head</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
action.accept(p.<span class="ST1">e</span>);
p = p.<span class="ST1">r</span>;
}
}
@Override
<span class="literal">public</span> String <span class="ST4">toString</span>() {
String s = <span class="string">&quot;</span><span class="string">PriorityQueueASDV {</span><span class="string">&quot;</span>;
Node&lt;E&gt; p = <span class="ST1">head</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
s += p.<span class="ST1">e</span>.toString();
<span class="literal">if</span> (p != <span class="ST1">tail</span>) {
s += <span class="string">&quot;</span><span class="string">, </span><span class="string">&quot;</span>;
}
p = p.<span class="ST1">r</span>;
}
s += <span class="string">&quot;</span><span class="string">}</span><span class="string">&quot;</span>;
<span class="literal">return</span> s;
}
@Override
<span class="literal">protected</span> Object <span class="ST4">clone</span>()
<span class="literal">throws</span> CloneNotSupportedException {
PriorityQueueASDV&lt;E&gt; c = (PriorityQueueASDV&lt;E&gt;) <span class="literal">super</span>.clone();
<span class="literal">return</span> c;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST7">main</span>(String[] args) {
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">--&gt; PriorityQueueASDV testing add</span><span class="string">&quot;</span>);
PriorityQueueASDV&lt;String&gt; pq1 = <span class="literal">new</span> PriorityQueueASDV();
pq1.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">--&gt; Colllections PriorityQueue testing add</span><span class="string">&quot;</span>);
PriorityQueue&lt;String&gt; pq2 = <span class="literal">new</span> PriorityQueue();
pq2.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq2);
<span class="comment">//TEST IT FULLY HERE. FOR ALL METHODS AND ALL CASES.</span>
<span class="comment">//Have the Jzva PriorityQueue below</span>
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">--&gt; PriorityQueueASDV testing remove(object o)</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from front Athens</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from end Paris</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from the middle Lafayette</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">add at the end Stocholm</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from the middle London</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from the front Berlin</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from the front/end Stocholm</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from empty queue</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">--&gt; PriorityQueueASDV recreate priority queues from empty</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">+++HERE YOU TEST ALL YOUR METHODS FULLY, and the methods of Colleciion PriorityQueue</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> offer New York</span><span class="string">&quot;</span>);
pq1.offer(<span class="string">&quot;</span><span class="string">New York</span><span class="string">&quot;</span>);
pq2.offer(<span class="string">&quot;</span><span class="string">New York</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> offer Miami</span><span class="string">&quot;</span>);
pq1.offer(<span class="string">&quot;</span><span class="string">Miami</span><span class="string">&quot;</span>);
pq2.offer(<span class="string">&quot;</span><span class="string">Miami</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> offer null</span><span class="string">&quot;</span>);
<span class="literal">try</span> {
pq1.offer(<span class="literal">n</span><span class="literal">ull</span>);
} <span class="literal">catch</span> (Exception e) {
System.<span class="ST6">err</span>.println(e);
}
<span class="literal">try</span> {
pq2.offer(<span class="literal">n</span><span class="literal">ull</span>);
} <span class="literal">catch</span> (Exception e) {
System.<span class="ST6">err</span>.println(e);
}
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> offer ClassCastException with Object</span><span class="string">&quot;</span>);
<span class="literal">try</span> {
pq1.offer((String) <span class="literal">new</span> Object());
} <span class="literal">catch</span> (Exception e) {
System.<span class="ST6">err</span>.println(e);
}
<span class="literal">try</span> {
pq2.offer((String) <span class="literal">new</span> Object());
} <span class="literal">catch</span> (Exception e) {
System.<span class="ST6">err</span>.println(e);
}
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> poll suposed to be Athens</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1.poll());
System.<span class="ST6">out</span>.println(pq2.poll());
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> Iterator</span><span class="string">&quot;</span>);
Iterator&lt;String&gt; it1 = pq1.iterator();
Iterator&lt;String&gt; it2 = pq2.iterator();
<span class="literal">while</span> (it1.hasNext()) {
System.<span class="ST6">out</span>.print(it1.next() + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
<span class="literal">while</span> (it2.hasNext()) {
System.<span class="ST6">out</span>.print(it2.next() + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> Iterator NoSuchElementException </span><span class="string">&quot;</span>);
<span class="literal">try</span> {
System.<span class="ST6">out</span>.println(it1.next());
} <span class="literal">catch</span> (NoSuchElementException e) {
System.<span class="ST6">err</span>.println(e);
}
<span class="literal">try</span> {
System.<span class="ST6">out</span>.println(it2.next());
} <span class="literal">catch</span> (NoSuchElementException e) {
System.<span class="ST6">err</span>.println(e);
}
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> Iterator foreach </span><span class="string">&quot;</span>);
it1 = pq1.iterator();
it2 = pq2.iterator();
it1.forEachRemaining(<span class="literal">new</span> Consumer() {
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">accept</span>(Object t) {
System.<span class="ST6">out</span>.print(t + <span class="string">&quot;</span><span class="string">*** </span><span class="string">&quot;</span>);
}
});
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
it2.forEachRemaining(<span class="literal">new</span> Consumer() {
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">accept</span>(Object t) {
System.<span class="ST6">out</span>.print(t + <span class="string">&quot;</span><span class="string">+++ </span><span class="string">&quot;</span>);
}
});
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> addAll Houston Chicago</span><span class="string">&quot;</span>);
List&lt;String&gt; ar1 = Arrays.<span class="ST5">asList</span>(<span class="string">&quot;</span><span class="string">Houston</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Chicago</span><span class="string">&quot;</span>);
pq1.addAll(ar1);
pq2.addAll(ar1);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> clear</span><span class="string">&quot;</span>);
pq1.clear();
pq2.clear();
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">--&gt; PriorityQueueASDV recreate priority queues from empty</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> forEach</span><span class="string">&quot;</span>);
pq1.forEach(<span class="literal">new</span> Consumer() {
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">accept</span>(Object t) {
System.<span class="ST6">out</span>.print(t + <span class="string">&quot;</span><span class="string">*** </span><span class="string">&quot;</span>);
}
});
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
pq2.forEach(<span class="literal">new</span> Consumer() {
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">accept</span>(Object t) {
System.<span class="ST6">out</span>.print(t + <span class="string">&quot;</span><span class="string">+++ </span><span class="string">&quot;</span>);
}
});
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> clone</span><span class="string">&quot;</span>);
<span class="literal">try</span> {
PriorityQueueASDV&lt;String&gt; pq1Cloned
= (PriorityQueueASDV&lt;String&gt;) pq1.clone();
System.<span class="ST6">out</span>.println(pq1Cloned);
pq1Cloned.add(<span class="string">&quot;</span><span class="string">Las Vegas</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1Cloned);
System.<span class="ST6">out</span>.println(pq1);
} <span class="literal">catch</span> (CloneNotSupportedException e) {
System.<span class="ST6">err</span>.println(e);
}
pq1.clear();
pq2.clear();
pq1.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">----------------</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Attempt to remove an element.</span><span class="string">&quot;</span>);
pq1.remove();
pq2.remove();
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Get array of the priority queues.</span><span class="string">&quot;</span>);
Object pqArray1[] = pq1.toArray();
Object pqArray2[] = pq2.toArray();
<span class="ST5">printArrays</span>(pqArray1);
<span class="ST5">printArrays</span>(pqArray2);
System.<span class="ST6">out</span>.println();
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">----------------</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Test .toArray(T[])</span><span class="string">&quot;</span>);
String[] pqArray3 = pq1.toArray(<span class="literal">new</span> String[<span class="number">0</span>]);
<span class="ST5">printArrays</span>(pqArray3);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">----------------</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Test containsAll()</span><span class="string">&quot;</span>);
ArrayList&lt;String&gt; testArray = <span class="literal">new</span> ArrayList&lt;&gt;();
testArray.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
testArray.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
testArray.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Does pq1 contain Lafayette, Berlin, and Zurich? </span><span class="string">&quot;</span> + (pq1.containsAll(testArray) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>));
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Does pq1 contain the contents of pq2? </span><span class="string">&quot;</span> + (pq1.containsAll(pq2) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>));
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Does pq2 contain the contents of pq1? </span><span class="string">&quot;</span> + (pq2.containsAll(pq1) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>));
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Adding funkytown to testArray...</span><span class="string">&quot;</span>);
testArray.add(<span class="string">&quot;</span><span class="string">Funkytown</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Does pq1 contain Lafayette, Berlin, Zurich, and Funkytown? </span><span class="string">&quot;</span> + (pq1.containsAll(testArray) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>));
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Test if containsAll() correctly throws a NullPointerException...</span><span class="string">&quot;</span>);
<span class="literal">try</span> {
testArray.add(<span class="literal">n</span><span class="literal">ull</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Does pq1 contain Lafayette, Berlin, Zurich, Funkytown, and null? </span><span class="string">&quot;</span> + (pq1.containsAll(testArray) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>));
} <span class="literal">catch</span> (NullPointerException ex) {
System.<span class="ST6">out</span>.println(ex);
}
testArray.remove(<span class="literal">n</span><span class="literal">ull</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">That worked! Continuing with tests...</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">----------------</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Testing removeAll(Collection&lt;?&gt;)...</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Removing the elements in the test array...</span><span class="string">&quot;</span>);
pq1.removeAll(testArray);
pq2.removeAll(testArray);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">----------------</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Testing retainAll()...</span><span class="string">&quot;</span>);
ArrayList&lt;String&gt; testArray2 = <span class="literal">new</span> ArrayList&lt;&gt;();
testArray2.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
testArray2.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq1.retainAll(testArray2);
pq2.retainAll(testArray2);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
}
<span class="literal">static</span> <span class="literal">void</span> <span class="ST7">printArrays</span>(Object[] arr) {
<span class="literal">for</span> (Object element : arr) {
System.<span class="ST6">out</span>.print(element + <span class="string">&quot;</span><span class="string">, </span><span class="string">&quot;</span>);
}
}
}
</pre></body>
</html>

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>com.calebfontenot</groupId>
<artifactId>ProgrammingExam2_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>com.calebfontenot.programmingexam2_calebfontenot.ProgrammingExam2_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,144 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.calebfontenot.programmingexam2_calebfontenot;
/**
*
* @author caleb
*/
/**
*
* @author ar114
* @param <T>
*/
public class ProgrammingExam2_CalebFontenot<T> {
Node<T> head;
Node<T> tail;
class Node<T> {
T t;
Node<T> l;
Node<T> r;
}
public T removeAt(int pos) {
if (pos < 0 || pos > size() - 1) {
throw new IndexOutOfBoundsException();
}
//list is empty
if (size() == 0) {
throw new RuntimeException("The list empty.");
}
T returnT = null;
if (pos == 0)//remove at 0
{
Node<T> pointer = head.r;
returnT = (T) head.t;
pointer.l = null;
head = pointer;
} else if (pos == (size() - 1))//remove at end
{
Node<T> pointer = tail.l.l;
returnT = (T) tail.t;
pointer.r = null;
tail = pointer;
} else//remove in the middle
{
// Iterate to the element position
Node<T> pointer = head;
for (int i = 0; i < pos - 1; ++i) {
pointer = pointer.r;
}
returnT = (T) pointer.r.t;
pointer.r = pointer.r.r;
pointer.l = pointer.r;
}
return returnT;
}
public void clear() {
head = tail = null;
}
public void addAt(T t, int pos) {
if (pos < 0 || pos > size()) {
throw new IndexOutOfBoundsException();
}
Node<T> newNode = new Node<T>();
newNode.t = t;
if (head == null)//list is empty
{
head = tail = newNode;
} else if (pos == 0)//add at the front
{
newNode.r = head;
head.l = newNode;
head = newNode;
} else if (pos == size())//add at the end
{
newNode.l = tail;
tail.r = newNode;
tail = newNode;
} else//middle
{
Node<T> p = head;
for (int i = 0; i < pos - 1; ++i) {
p = p.r;
}
newNode.l = p;
newNode.r = p.r;
p.r.l = newNode;
p.r = newNode;
}
}
public int size() {
Node<T> p = head;
int count = 0;
while (p != null) {
count++;
p = p.r;
}
return count;
}
@Override
public String toString() {
String s = "";
Node<T> p = head;
while (p != null) {
s += p.t.toString() + " ";
p = p.r;
}
return s;
}
public static void main(String[] args) {
ProgrammingExam2_CalebFontenot<Integer> list = new ProgrammingExam2_CalebFontenot<Integer>();
list.addAt(20, 0);
list.addAt(10, 0);
list.addAt(40, 2);
list.addAt(30, 2);
list.addAt(50, 4);
System.out.println(list);
System.out.println(list.removeAt(0));
System.out.println(list);
System.out.println(list.removeAt(2));
System.out.println(list);
System.out.println(list.removeAt(2));
System.out.println(list.toString());
}
}

View File

@@ -0,0 +1,155 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>GenericMergeSortExam.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #000000; background-color: #ffffff; font-family: monospace}
pre {color: #000000; background-color: #ffffff; font-family: monospace}
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
.ST1 {color: #969696; font-family: monospace; font-weight: bold}
.ST2 {font-family: monospace; font-weight: bold}
.comment {color: #969696}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
.ST4 {font-family: monospace; font-style: italic}
.ST6 {color: #ce54b8; font-family: monospace; font-style: italic}
.ST0 {color: #287bde}
.ST5 {color: #ce54b8}
.string {color: #1e9347}
.literal {color: #336bdd}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">C:\Users\ar114\Documents\NetBeansProjects\ProgramingExam2_CalebFontenot\src\main\java\com\mycompany\programingexam2_calebfontenot\GenericMergeSortExam.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.mycompany.programingexam2_calebfontenot;
<span class="literal">import</span> java.lang.reflect.Array;
<span class="literal">import</span> java.util.Comparator;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">ar114</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">GenericMergeSortExam</span> {
<span class="literal">public</span> <span class="literal">static</span> &lt;E <span class="literal">extends</span> Comparable&lt;E&gt;&gt; <span class="literal">void</span> <span class="ST3">mergeSort</span>(E[] list) {
<span class="ST4">mergeSort</span>(list,
<span class="literal">new</span> Comparator&lt;E&gt;() {
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">compare</span>(E e1, E e2) {
<span class="literal">int</span> compareResult = ((Comparable&lt;E&gt;) e1).compareTo(e2);
<span class="literal">return</span> compareResult;
}
});
}
<span class="literal">public</span> <span class="literal">static</span> &lt;E&gt; <span class="literal">void</span> <span class="ST3">mergeSort</span>(E[] list,
Comparator&lt;? <span class="literal">super</span> E&gt; comparator) {
<span class="literal">if</span> (list.<span class="ST5">length</span> &gt; 1) {
<span class="comment">// Merge sort the first half</span>
E[] firstHalf = (E[]) <span class="literal">new</span> Object[list.<span class="ST5">length</span> / 2];
<span class="comment">//copies 1st half of list into array firstHalf</span>
System.<span class="ST4">arraycopy</span>(list, 0, firstHalf, 0, list.<span class="ST5">length</span> / 2);
<span class="ST4">mergeSort</span>(firstHalf, comparator);
<span class="comment">// Merge sort the second half</span>
<span class="literal">int</span> secondHalfLength = list.<span class="ST5">length</span> - list.<span class="ST5">length</span> / 2;
E[] secondHalf = (E[]) <span class="literal">new</span> Object[secondHalfLength];
System.<span class="ST4">arraycopy</span>(list, list.<span class="ST5">length</span> / 2,
secondHalf, 0, secondHalfLength);
<span class="ST4">mergeSort</span>(secondHalf, comparator);
<span class="comment">// Merge firstHalf with secondHalf</span>
E[] temp = <span class="ST4">merge1</span>(firstHalf, secondHalf, comparator);
System.<span class="ST4">arraycopy</span>(temp, 0, list, 0, temp.<span class="ST5">length</span>);
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST1">Merges</span> <span class="ST1">the</span> <span class="ST1">two</span> <span class="ST1">lists</span> <span class="ST1">using</span> <span class="ST1">a</span> <span class="ST1">Comparator</span><span class="ST1">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST1">@param</span> &lt;E&gt; <span class="comment">The</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">the</span> <span class="comment">methods</span> <span class="comment">accepts</span><span class="comment">.</span>
<span class="comment"> * </span><span class="ST1">@param</span> list1 <span class="comment">a</span> <span class="comment">list</span> <span class="comment">of</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">E</span>
<span class="comment"> * </span><span class="ST1">@param</span> list2 <span class="comment">a</span> <span class="comment">list</span> <span class="comment">of</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">E</span>
<span class="comment"> * </span><span class="ST1">@param</span> comparator <span class="comment">Comparator</span>
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">a</span> <span class="comment">sorted</span> <span class="comment">new</span> <span class="comment">list</span> <span class="comment">made</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">merged</span> <span class="comment">list1</span> <span class="comment">and</span> <span class="comment">list2</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">static</span> &lt;E&gt; E[]
<span class="ST3">merge1</span>(E[] list1, E[] list2, Comparator&lt;? <span class="literal">super</span> E&gt; comparator) {
<span class="literal">int</span> returnArraySize = list1.<span class="ST5">length</span> + list2.<span class="ST5">length</span>;
E[] returnArray = (E[]) <span class="literal">new</span> Object[returnArraySize];
<span class="literal">int</span> list1Iterator = 0, list2Iterator = 0, returnArrayIterator = 0, iterationsRemaining = 0;
<span class="literal">for</span> (; returnArrayIterator &lt; returnArraySize; ++returnArrayIterator) {
iterationsRemaining = (returnArraySize - returnArrayIterator);
<span class="literal">if</span> (comparator.compare(list1[list1Iterator], list2[list2Iterator]) &lt;= 0) {
returnArray[returnArrayIterator] = list1[list1Iterator];
<span class="literal">if</span> (iterationsRemaining &gt; 1)
returnArray[returnArrayIterator + 1] = list2[list2Iterator];
<span class="literal">if</span> ((list1.<span class="ST5">length</span> - 1) &lt; (list1Iterator)) {
++list1Iterator;
} <span class="literal">else</span> {
++list2Iterator;
}
++returnArrayIterator;
} <span class="literal">else</span> {
returnArray[returnArrayIterator] = list2[list2Iterator];
<span class="literal">if</span> (iterationsRemaining &gt; 1)
returnArray[returnArrayIterator + 1] = list1[list1Iterator];
<span class="literal">if</span> ((list2.<span class="ST5">length</span> - 1) &lt; (list2Iterator)) {
++list2Iterator;
} <span class="literal">else</span> {
++list1Iterator;
}
++returnArrayIterator;
}
<span class="comment">/*</span>
<span class="comment"> if (comparator.compare(list1[list1Iterator], list2[list2Iterator]) == 1) {</span>
<span class="comment"> returnArray[returnArrayIterator] = list2[list2Iterator];</span>
<span class="comment"> ++list2Iterator;</span>
<span class="comment"> ++returnArrayIterator;</span>
<span class="comment"> } else {</span>
<span class="comment"> returnArray[returnArrayIterator] = list1[list1Iterator];</span>
<span class="comment"> ++list1Iterator;</span>
<span class="comment"> ++returnArrayIterator;</span>
<span class="comment"> }</span>
<span class="comment"> */</span>
}
<span class="literal">return</span> returnArray;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">main</span>(String[] args) {
Integer[] list
= {
2, 3, 2, 5, 6, 1, -2, 3, 14, 12
};
<span class="ST4">mergeSort</span>(list);
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt; list.<span class="ST5">length</span>; i++) {
System.<span class="ST6">out</span>.print(list[i] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST6">out</span>.println();
String[] list1
= {
<span class="string">&quot;</span><span class="string">ABC</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">abc</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">abm</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Anf</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Good</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Bad</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">nice</span><span class="string">&quot;</span>
};
<span class="ST4">mergeSort</span>(list1, <span class="literal">new</span> Comparator&lt;String&gt;() {
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">compare</span>(String s1, String s2) {
<span class="literal">return</span> s1.compareToIgnoreCase(s2);
}
});
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt; list1.<span class="ST5">length</span>; i++) {
System.<span class="ST6">out</span>.print(list1[i] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,165 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>ProgramingExam2_CalebFontenot.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #000000; background-color: #ffffff; font-family: monospace}
pre {color: #000000; background-color: #ffffff; font-family: monospace}
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
.ST1 {color: #969696; font-family: monospace; font-weight: bold}
.ST2 {font-family: monospace; font-weight: bold}
.comment {color: #969696}
.ST4 {font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #ce54b8; font-family: monospace; font-style: italic}
.ST0 {color: #287bde}
.ST3 {color: #ce54b8}
.string {color: #1e9347}
.literal {color: #336bdd}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">C:\Users\ar114\Documents\NetBeansProjects\ProgramingExam2_CalebFontenot\src\main\java\com\mycompany\programingexam2_calebfontenot\ProgramingExam2_CalebFontenot.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/$</span><span class="comment">{packagePath}/${mainClassName}.java to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.mycompany.programingexam2_calebfontenot;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">ar114</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">ProgramingExam2_CalebFontenot</span>&lt;T&gt; {
Node&lt;T&gt; <span class="ST3">head</span>;
Node&lt;T&gt; <span class="ST3">tail</span>;
<span class="literal">class</span> <span class="ST2">Node</span>&lt;T&gt; {
T <span class="ST3">t</span>;
Node&lt;T&gt; <span class="ST3">l</span>;
Node&lt;T&gt; <span class="ST3">r</span>;
}
<span class="literal">public</span> T <span class="ST2">removeAt</span>(<span class="literal">int</span> pos) {
<span class="literal">if</span> (pos &lt; 0 || pos &gt; size() - 1) {
<span class="literal">throw</span> <span class="literal">new</span> IndexOutOfBoundsException();
}
<span class="comment">//list is empty</span>
<span class="literal">if</span> (size() == 0) {
<span class="literal">throw</span> <span class="literal">new</span> RuntimeException(<span class="string">&quot;</span><span class="string">The list empty.</span><span class="string">&quot;</span>);
}
T returnT = <span class="literal">null</span>;
<span class="literal">if</span> (pos == 0)<span class="comment">//remove at 0</span>
{
Node&lt;T&gt; pointer = <span class="ST3">head</span>.<span class="ST3">r</span>;
returnT = (T) <span class="ST3">head</span>.<span class="ST3">t</span>;
pointer.<span class="ST3">l</span> = <span class="literal">null</span>;
<span class="ST3">head</span> = pointer;
} <span class="literal">else</span> <span class="literal">if</span> (pos == (size() - 1))<span class="comment">//remove at end</span>
{
Node&lt;T&gt; pointer = <span class="ST3">tail</span>.<span class="ST3">l</span>.<span class="ST3">l</span>;
returnT = (T) <span class="ST3">tail</span>.<span class="ST3">t</span>;
pointer.<span class="ST3">r</span> = <span class="literal">null</span>;
<span class="ST3">tail</span> = pointer;
} <span class="literal">else</span><span class="comment">//remove in the middle</span>
{
<span class="comment">// Iterate to the element position</span>
Node&lt;T&gt; pointer = <span class="ST3">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt; pos - 1; ++i) {
pointer = pointer.<span class="ST3">r</span>;
}
pointer.<span class="ST3">r</span> = pointer.<span class="ST3">r</span>.<span class="ST3">r</span>;
pointer.<span class="ST3">l</span> = pointer.<span class="ST3">r</span>;
returnT = (T) pointer.<span class="ST3">t</span>;
}
<span class="literal">return</span> returnT;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">clear</span>() {
<span class="ST3">head</span> = <span class="ST3">tail</span> = <span class="literal">null</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">addAt</span>(T t, <span class="literal">int</span> pos) {
<span class="literal">if</span> (pos &lt; 0 || pos &gt; size()) {
<span class="literal">throw</span> <span class="literal">new</span> IndexOutOfBoundsException();
}
Node&lt;T&gt; newNode = <span class="literal">new</span> Node&lt;T&gt;();
newNode.<span class="ST3">t</span> = t;
<span class="literal">if</span> (<span class="ST3">head</span> == <span class="literal">null</span>)<span class="comment">//list is empty</span>
{
<span class="ST3">head</span> = <span class="ST3">tail</span> = newNode;
} <span class="literal">else</span> <span class="literal">if</span> (pos == 0)<span class="comment">//add at the front</span>
{
newNode.<span class="ST3">r</span> = <span class="ST3">head</span>;
<span class="ST3">head</span>.<span class="ST3">l</span> = newNode;
<span class="ST3">head</span> = newNode;
} <span class="literal">else</span> <span class="literal">if</span> (pos == size())<span class="comment">//add at the end</span>
{
newNode.<span class="ST3">l</span> = <span class="ST3">tail</span>;
<span class="ST3">tail</span>.<span class="ST3">r</span> = newNode;
<span class="ST3">tail</span> = newNode;
} <span class="literal">else</span><span class="comment">//middle</span>
{
Node&lt;T&gt; p = <span class="ST3">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt; pos - 1; ++i) {
p = p.<span class="ST3">r</span>;
}
newNode.<span class="ST3">l</span> = p;
newNode.<span class="ST3">r</span> = p.<span class="ST3">r</span>;
p.<span class="ST3">r</span>.<span class="ST3">l</span> = newNode;
p.<span class="ST3">r</span> = newNode;
}
}
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">size</span>() {
Node&lt;T&gt; p = <span class="ST3">head</span>;
<span class="literal">int</span> count = 0;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
count++;
p = p.<span class="ST3">r</span>;
}
<span class="literal">return</span> count;
}
@Override
<span class="literal">public</span> String <span class="ST2">toString</span>() {
String s = <span class="string">&quot;&quot;</span>;
Node&lt;T&gt; p = <span class="ST3">head</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
s += p.<span class="ST3">t</span>.toString() + <span class="string">&quot;</span> <span class="string">&quot;</span>;
p = p.<span class="ST3">r</span>;
}
<span class="literal">return</span> s;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">main</span>(String[] args) {
ProgramingExam2_CalebFontenot&lt;Integer&gt; list = <span class="literal">new</span> ProgramingExam2_CalebFontenot&lt;Integer&gt;();
list.addAt(20, 0);
list.addAt(10, 0);
list.addAt(40, 2);
list.addAt(30, 2);
list.addAt(50, 4);
System.<span class="ST5">out</span>.println(list);
System.<span class="ST5">out</span>.println(list.removeAt(0));
System.<span class="ST5">out</span>.println(list);
System.<span class="ST5">out</span>.println(list.removeAt(2));
System.<span class="ST5">out</span>.println(list);
System.<span class="ST5">out</span>.println(list.removeAt(2));
System.<span class="ST5">out</span>.println(list.toString());
}
}
</pre></body>
</html>

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>com.mycompany.programmingquiz1_calebfontenot</groupId>
<artifactId>ProgrammingQuiz1_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<exec.mainClass>com.mycompany.programmingquiz1_calebfontenot.ProgrammingQuiz1_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,101 @@
/*
* 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 com.mycompany.programmingquiz1_calebfontenot;
/**
*
* @author ar114
* @param <T>
*/
public class DoubleLinkedListExam<T> {
Node<T> head;
Node<T> tail;
class Node<T> {
T t;
Node<T> l;
Node<T> r;
}
public T removeAt(int pos) {
return null;
}
public void clear() {
head = tail = null;
}
public void addAt(T t, int pos) {
System.out.println("Linked List size: " + this.size());
if (pos == 0) { // add first element
Node<T> newNode = new Node<T>();
newNode.t = t;
head = tail = newNode;
return;
}
if (pos == this.size()) { // add at end
Node<T> tmp = tail;
Node<T> newNode = new Node<T>();
newNode.t = t;
tmp.r = newNode;
newNode.l = tmp;
newNode.r = null;
tail = newNode;
return;
}
if (pos < this.size()) { // add in middle
Node<T> pointer = head;
for (int i = 0; i < this.size() - 1; ++i) {
pointer = pointer.r;
}
Node<T> newNode = new Node<T>();
newNode.t = t;
newNode.l = pointer;
newNode.r = pointer.l;
pointer.r = newNode;
pointer.r.l = newNode;
return;
}
if (pos < 0 || this.size() < pos) {
throw new IndexOutOfBoundsException("Given index is outside the range of acceptible values.");
}
}
public int size() {
Node<T> p = head;
int count = 0;
while (p != null) {
count++;
p = p.r;
}
return count;
}
@Override
public String toString() {
String s = "";
Node<T> p = head;
while (p != null) {
s += p.t.toString() + " ";
p = p.r;
}
return s;
}
public static void main(String[] args) {
DoubleLinkedListExam<Integer> list = new DoubleLinkedListExam<>();
list.addAt(10, 0);
System.out.println(list);
list.addAt(30, 1);
list.addAt(20, 1);
list.addAt(5, 0);
System.out.println(list);
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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 com.mycompany.programmingquiz1_calebfontenot;
/**
*
* @author ar114
*/
public class InsertionSort {
public static void insertionSort(String[] list)
{
int i = 1;
while (i < list.length)
{
if (list[i - 1].compareTo(list[i]) >= 0) {
String tmp = list[i - 1];
list[i - 1] = list[i];
list[i] = tmp;
}
++i;
//1. Loop that opens the hole by shifting to the right
//2. End of loop of shifting ---Insert the current at the opened hole list[j+1]
}
}
public static void printArray(String[] array) {
for (String current: array) {
System.out.print(current + " ");
}
System.out.println();
}
public static void main(String[] args) {
String[] array = {"Zedfrey", "Hello", "Glorious", "World"};
printArray(array);
insertionSort(array);
printArray(array);
}
}

View File

@@ -0,0 +1,17 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
*/
package com.mycompany.programmingquiz1_calebfontenot;
/**
*
* @author ar114
*/
public class ProgrammingQuiz1_CalebFontenot {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

View File

@@ -0,0 +1,5 @@
#Generated by Maven
#Sun Dec 03 12:52:32 CST 2023
artifactId=ProgrammingQuiz1_CalebFontenot
groupId=com.mycompany.programmingquiz1_calebfontenot
version=1.0-SNAPSHOT

View File

@@ -0,0 +1,4 @@
com/mycompany/programmingquiz1_calebfontenot/InsertionSort.class
com/mycompany/programmingquiz1_calebfontenot/DoubleLinkedListExam.class
com/mycompany/programmingquiz1_calebfontenot/ProgrammingQuiz1_CalebFontenot.class
com/mycompany/programmingquiz1_calebfontenot/DoubleLinkedListExam$Node.class

View File

@@ -0,0 +1,3 @@
/home/caleb/ASDV-Java/Semester 3/Exams/Projects/ProgrammingQuiz1_CalebFontenot/src/main/java/com/mycompany/programmingquiz1_calebfontenot/ProgrammingQuiz1_CalebFontenot.java
/home/caleb/ASDV-Java/Semester 3/Exams/Projects/ProgrammingQuiz1_CalebFontenot/src/main/java/com/mycompany/programmingquiz1_calebfontenot/InsertionSort.java
/home/caleb/ASDV-Java/Semester 3/Exams/Projects/ProgrammingQuiz1_CalebFontenot/src/main/java/com/mycompany/programmingquiz1_calebfontenot/DoubleLinkedListExam.java

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>DAO</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.dao.DAO</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,20 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package DAO;
import java.util.Collection;
/**
*
* @author caleb
*/
public interface DAO<T> {
public void create(T t);
public void delete(T t);
public void update(T t);
public T find (T t);
public Collection<T> findALl();
public boolean updateDBase();
}

View File

@@ -0,0 +1,19 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package DAO;
/**
*
* @author caleb
*/
public interface Exam<T> {
void editExam(T t);
void takeExam(T t);
void submitExam(T t);
void previewExam(T t);
void monitorExam(T t);
void gradeExam(T t);
}

View File

@@ -0,0 +1,13 @@
/*
* 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 DAO;
/**
*
* @author caleb
*/
public abstract class ExamFactory<T extends Keyable> {
public abstract Exam<T> createExam();
}

View File

@@ -0,0 +1,56 @@
/*
* 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 DAO;
/**
*
* @author caleb
*/
public class ExamFactoryOutlet<K, V extends Keyable> extends ExamFactory<Keyable> {
@Override
public Exam<Keyable> createExam()
{
return new Exam<Keyable>() {
@Override
public void editExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void takeExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void submitExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void previewExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void monitorExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void gradeExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
};
}
}

View File

@@ -0,0 +1,15 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package DAO;
/**
*
* @author caleb
*/
public interface Keyable<K> {
public K getKey();
public void setKey(K key);
}

View File

@@ -0,0 +1,32 @@
/*
* 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 DAO;
import java.util.ArrayList;
/**
*
* @author caleb
*/
public class MathExam implements Keyable<MathExam> {
ArrayList<Object> fields = new ArrayList<Object>();
int key;
public MathExam(int key, ArrayList<Object> fields) {
}
@Override
public MathExam getKey()
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void setKey(MathExam key)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
}

View File

@@ -0,0 +1,23 @@
/*
* 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 DAO;
/**
*
* @author caleb
*/
public class Test {
public static void main(String[] args)
{
ExamFactory<MathExam> efm = new ExamFactory<MathExam>() {
@Override
public Exam<MathExam> createExam()
{
System.out.println("Hello World");
}
};
efm.createExam();
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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.dao.factory_pizza;
/**
*
* @author caleb
*/
public class CheezePizza implements Pizza {
@Override
public void prepare()
{
System.out.println("preparing Cheeze Pizza");
}
@Override
public void bake()
{
System.out.println("baking Cheeze Pizza");
}
@Override
public void cut()
{
System.out.println("cutting Cheeze Pizza");
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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.dao.factory_pizza;
/**
*
* @author caleb
*/
public class ChickenPizza implements Pizza {
@Override
public void prepare()
{
System.out.println("preparing Chicken Pizza");
}
@Override
public void bake()
{
System.out.println("baking Chicken Pizza");
}
@Override
public void cut()
{
System.out.println("cutting Chicken Pizza");
}
}

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package edu.slcc.asdv.caleb.dao.factory_pizza;
/**
*
* @author caleb
*/
public interface Pizza<T> {
public void prepare(T t);
public void bake(T t);
public void cut(T t);
}

View File

@@ -0,0 +1,29 @@
/*
* 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.dao.factory_pizza;
/**
*
* @author caleb
*/
public class PizzaFactory {
public PizzaFactory(String cheeze)
{
}
public static <T> Pizza<T> createPizza(T type) throws Exception {
Pizza p = null;
if (type.equals("cheeze"))
p = new CheezePizza();
else if (type.equals("chicken"))
p = new ChickenPizza();
else if (type.equals("veggie"))
p = new VeggiePizza();
if (p == null) {
throw new Exception("Not a valid Pizza.");
}
return p;
}
}

View File

@@ -0,0 +1,19 @@
/*
* 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.dao.factory_pizza;
/**
*
* @author caleb
*/
public class PizzaStore {
public Pizza orderPizza(String type) throws Exception {
Pizza p = PizzaFactory.createPizza(type);
p.prepare();
p.bake();
p.cut();
return p;
}
}

View File

@@ -0,0 +1,19 @@
/*
* 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.dao.factory_pizza;
/**
*
* @author caleb
*/
public class TestPizza {
public static void main(String[] args) throws Exception
{
PizzaStore ps = new PizzaStore();
ps.orderPizza("cheeze");
ps.orderPizza("chicken");
ps.orderPizza("veggie");
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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.dao.factory_pizza;
/**
*
* @author caleb
*/
public class VeggiePizza implements Pizza {
@Override
public void prepare()
{
System.out.println("Preparing Veggie Pizza");
}
@Override
public void bake()
{
System.out.println("Baking Veggie Pizza");
}
@Override
public void cut()
{
System.out.println("Cutting Veggie Pizza");
}
}

View File

@@ -0,0 +1,29 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package factory_generic_pizza;
import edu.slcc.asdv.caleb.dao.factory_pizza.*;
/**
*
* @author caleb
*/
public class Pizza<T> {
public void prepare(Object t)
{
System.out.println("preparing " + t.toString() +" Pizza");
}
public void bake(Object t)
{
System.out.println("baking " + t.toString() +" Pizza");
}
public void cut(Object t)
{
System.out.println("cutting" + t.toString() +"Pizza");
}
}

View File

@@ -0,0 +1,32 @@
/*
* 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 factory_generic_pizza;
import edu.slcc.asdv.caleb.dao.factory_pizza.*;
/**
*
* @author caleb
*/
public class PizzaFactory {
public static <T> Pizza<T> createPizza(T t)
{
return new Pizza<T>()
{
public void prepare(T t) {
System.out.println("Preparing pizza " + t.toString() + "...");
}
public void bake(T t) {
System.out.println("Baking pizza " + t.toString() + "...");
}
public void cut(T t) {
System.out.println("Cutting pizza " + t.toString() + "...");
}
};
}
}

View File

@@ -0,0 +1,21 @@
/*
* 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 factory_generic_pizza;
import edu.slcc.asdv.caleb.dao.factory_pizza.*;
/**
*
* @author caleb
*/
public class PizzaStore {
public Pizza orderPizza(String type) throws Exception {
Pizza p = PizzaFactory.createPizza(type);
p.prepare(type);
p.bake(type);
p.cut(type);
return p;
}
}

View File

@@ -0,0 +1,21 @@
/*
* 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 factory_generic_pizza;
import edu.slcc.asdv.caleb.dao.factory_pizza.*;
/**
*
* @author caleb
*/
public class TestPizza {
public static void main(String[] args) throws Exception
{
PizzaStore ps = new PizzaStore();
ps.orderPizza("cheeze");
ps.orderPizza("chicken");
ps.orderPizza("veggie");
}
}

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.jdkPlatform>JDK_1.8</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>

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>DataStructures</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.datastructures.DataStructures</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.datastructures;
/**
*
* @author caleb
*/
public class DataStructures {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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.datastructures;
import java.util.Collection;
import java.util.Set;
/**
*
* @author caleb
*/
public interface OneToMany<One, Many>
{
/**
* Initialization of Ones. The method should be used first before any other
* methods
*
* @param one - the ones ( i.e, countries, or SelectItem or any Object) to
* use for initialization
* @return true if the initialization succeeded by using the method once,
* false when the method is used more than once.
*/
boolean initializeOne(One... one);
/**
* Initialization of the many for a given one. The method can be used
* multiple times after the method initializeOne has succeeded.
*
* @param one - the one that has the many
* @param many - the many which belong to th eone
* @throws IllegalArgumentException when the one does not exist (i.e. user's
* typing error for the name of one) or when the initialization of the one
* has not occurred.
*
*/
void initializeMany(One one, Many... many)
throws IllegalArgumentException;
/**
* Gets the many of a specific one.
*
* @param one the one to get its many
* @return the many of the parameter one or null if the one does not exist.
*/
Collection<Many> getMany(One one);
/**
* Given a value of the many it gets the one that the many belongs to.
*
* @param many one of the values of the many
* @return the one
*/
One getOne(Many many);
/**
* Gets a set with all the ones
*
* @return the set of ones.
*/
Set<One> getAllOnes();
}

View File

@@ -0,0 +1,185 @@
package edu.slcc.asdv.caleb.datastructures;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class OneToManyFactory {
/**
* Creates an object of type OneToMany
*
* @param <One> a generic parameter can be any object that denotes a One.
* @param <Many> a generic parameter can be any object that denotes a city that belongs to the one generic type.
* @return a OneCity object.
*/
public static <One, Many> //generic types to be used in the method
OneToMany<One, Many> //rturn type
createOneToMany()
{
return new OneToMany<One, Many>() {
private Map<One, Many> oneToMany = new HashMap();
boolean oneInitialized = false;
boolean manyInitialized = false;
@Override
public boolean initializeOne(One... one)
{
if (oneInitialized == false && manyInitialized == false) {
for (int i = 0; i < one.length; ++i) {
oneToMany.put(one[i], (Many) new Boolean(true));
}
oneInitialized = true;
return true;
}
return false;
}
@Override
public void initializeMany(One one, Many... many)
throws IllegalArgumentException
{
if (oneToMany.get(one) == null) { // of the key of the one is null
// the method initializekey has not been used
throw new IllegalArgumentException(one + " is not valid.");
}
oneToMany.put(one, (Many) new ArrayList<Many>(Arrays.asList(many)));
manyInitialized = true;
}
@Override
public Collection<Many> getMany(One one)
throws IllegalArgumentException
{
if (oneInitialized == true && manyInitialized == true) {
if (oneToMany.get(one) == null) {
throw new IllegalArgumentException(one + " is not a valid One");
}
Collection c1 = (Collection) oneToMany.get(one);
return c1;
}
return null;
}
@Override
public One getOne(Many many)
{
Set< Entry<One, Many>> set = oneToMany.entrySet();
for (Map.Entry<One, Many> entry : oneToMany.entrySet()) {
One key = (One) entry.getKey();
Collection<Many> value = (Collection<Many>) oneToMany.get(key);
if (value.contains(many)) {
return key;
}
}
return null;
}
@Override
public Set<One> getAllOnes()
{
return (Set<One>) oneToMany.keySet();
}
};
}
public static void main(String[] args)
{
OneToMany cc = OneToManyFactory.createOneToMany();
try {
cc.initializeMany("France", "Paris");
} catch (Exception e) {
System.err.println(e);
}
boolean b1 = cc.initializeOne("USA", "Greece");
System.out.println(b1);
boolean b2 = cc.initializeOne("USA", "Greece");
System.out.println(b2);
cc.initializeMany("USA", "Lafayette", "New Orleans");
cc.initializeMany("Greece", "Athens", "Sparta");
Collection<String> cities1 = cc.getMany("USA");
System.out.println(cities1);
Collection<String> cities2 = cc.getMany("Greece");
System.out.println(cities2);
System.out.println(cc.getOne("Athens"));
System.out.println(cc.getOne("Lafayette"));
System.out.println(cc.getOne("France"));
try {
System.out.println(cc.getMany("Germany"));
} catch (Exception e) {
System.err.println(e);
}
System.out.println(cc.getAllOnes());
System.out.println("----------------------------------------------------------");
OneToMany supplierParts = OneToManyFactory.createOneToMany();
supplierParts.initializeOne(new Supplier("s1"), new Supplier("s2"));
Supplier s1 = new Supplier("s1");
Supplier s2 = new Supplier("s2");
supplierParts.initializeOne(s1, s2);
Part p1 = new Part("p1");
Part p2 = new Part("p2");
Part p3 = new Part("p3");
Part p4 = new Part("p4");
supplierParts.initializeMany(s1, p1, p2);
supplierParts.initializeMany(s2, p3, p4);
}
}
class Supplier {
public Supplier(String name)
{
this.name = name;
}
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String toString()
{
return "Supplier{" + "name=" + name + '}';
}
}
class Part {
public Part(String name)
{
this.name = name;
}
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String toString()
{
return "Part{" + "name=" + name + '}';
}
}

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>HashMapASDV</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.hashmapasdv.HashMapASDV</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.hashmapasdv;
/**
*
* @author caleb
*/
public class HashMapASDV {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

Binary file not shown.

View File

@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="HashMapASDV_Ant" default="default" basedir=".">
<description>Builds, tests, and runs the project HashMapASDV_Ant.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="HashMapASDV_Ant-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
build.xml.data.CRC32=2e2f5c1c
build.xml.script.CRC32=57d558c6
build.xml.stylesheet.CRC32=f85dc8f2@1.108.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=2e2f5c1c
nbproject/build-impl.xml.script.CRC32=4a925e01
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.108.0.48

View File

@@ -0,0 +1,99 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=HashMapASDV_Ant
application.vendor=caleb
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.modulepath=\
${run.modulepath}
debug.test.classpath=\
${run.test.classpath}
debug.test.modulepath=\
${run.test.modulepath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/HashMapASDV_Ant.jar
dist.javadoc.dir=${dist.dir}/javadoc
dist.jlink.dir=${dist.dir}/jlink
dist.jlink.output=${dist.jlink.dir}/HashMapASDV_Ant
endorsed.classpath=
excludes=
file.reference.ListASDV.jar=ListASDV.jar
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.source=20
javac.target=20
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.modulepath=\
${javac.modulepath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.html5=false
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
# The jlink additional root modules to resolve
jlink.additionalmodules=
# The jlink additional command line parameters
jlink.additionalparam=
jlink.launcher=true
jlink.launcher.name=HashMapASDV_Ant
main.class=hashmapasdv_ant.HashMapASDV_Ant
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}:\
${file.reference.ListASDV.jar}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.modulepath=\
${javac.modulepath}
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
run.test.modulepath=\
${javac.test.modulepath}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>HashMapASDV_Ant</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>

View File

@@ -0,0 +1,91 @@
/*
* 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 hashmapasdv_ant;
import java.util.Map.Entry;
import java.util.Objects;
/**
*
* @author caleb
*/
class EntryASDV<K, V> implements Entry<K, V>, Comparable<K>
{
K key;
V value;
public EntryASDV(K key, V value)
{
this.key = key;
this.value = value;
}
@Override
public K getKey()
{
return key;
}
@Override
public V getValue()
{
return value;
}
@Override
public V setValue(V value)
{
V oldValue = this.value;
this.value = value;
return oldValue;
}
@Override
public String toString()
{
return "EntryASDV{" + "key=" + key + ", value=" + value + '}';
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final EntryASDV<?, ?> other = (EntryASDV<?, ?>) obj;
if (!Objects.equals(this.key, other.key))
{
return false;
}
return true;
}
/**
*
* @param o
* @return throws IllegalArgumentException if parameter class is not K
*/
@Override
public int compareTo(K o)
{
if (getClass() != o.getClass())
{
throw new IllegalArgumentException("ellegal parameter " + o);
}
return ((Comparable) key).compareTo(o);
}
}

File diff suppressed because it is too large Load Diff

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>Hashing_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.hashing_calebfontenot.Hashing_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,791 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.hashing_calebfontenot;
/**
*
* @author caleb
*/
import asdv.ListASDV;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;//needed in putAll
public class MapASDV<K, V> implements Map<K, V>, Cloneable
{
private int capacity = 4;
private double loadFactor = 0.75;
private ArrayList<ListASDV<EntryASDV<K, V>>> map = new ArrayList<ListASDV<EntryASDV<K, V>>>();
private Set<K> sharedKeySet = new SharedSet<K>();
private Set<Entry<K, V>> sharedEntrySet = new SharedSet<Entry<K, V>>();
private Collection<V> sharedValuesCollection = new SharedCollection<V>();
private class SharedCollection<V> extends ArrayList<V>
{
public boolean addValue(V v)
{
return this.add(v);
}
@Override
public boolean remove(Object o)
{
//> The parameter is key not entry if we are here
//>>remove value) and key) from the map
return MapASDV.this.removeFirstValue(o);
//>>remove key from shared values set
//return super.remove(o);
}
/**
* Removes one value from the collection. This method is meant to be
* called from out class. The overridden remove(Object o) of this inner
* class, calls the remove of the outer class(MapASDV), and the remove
* of the outer class, calls remove(V v, boolean callFromOuterClass)
* instead of remove(Object o) to avoid Stack Overflow when remover of
* inner calls remove of outer and vice versa.
*
* @param o - the key
* @param callFromOuterClass - dummy variable.
* @return true if the key was removed from the Set
*/
public boolean remove(V v, boolean callFromOuterClass)
{
//remove key from shared keys set
boolean b = super.remove(v);
return b;
}
@Override
public Object clone()
{
return super.clone();
}
@Override
public void clear()
{
super.clear();
}
}
private class SharedSet<E> extends HashSet<E>
{
@Override
public boolean add(E e)
{
throw new UnsupportedOperationException("Not supported....");
}
@Override
public boolean removeAll(Collection<?> c)
{
throw new UnsupportedOperationException("Not supported....");
}
/**
* Adds an EntryASDV to the set. It is private and cannot be used by the
* user of the Set. It is used from the MapASDV to add EntriesASDV to
* the SharedSet. Without this method we wouldn't be able to create a
* Set of keys or a Set of entries to give to the user via methods
* keySet() and entrySet() of this Map
*
* @param e EntryASDV
* @return true if the entry was added false otherwise
*/
private boolean addEntry(E e)
{
return super.add(e);
}
@Override
public boolean remove(Object o)
{
//>if parameter oo is EntryASDV call auxiliary method removeEntry
if (o instanceof EntryASDV)
{
return removeEntry((EntryASDV) o);
}
//> The parameter is key not entry if we are here
//>>remove key from the map
MapASDV.this.remove(o);
//>>remove key from shared keys set
return super.remove(o);
}
/**
* Removes the entry for the shared set
*
* @param entry the entry to be removed
* @return true if the entry was removed, false otherwise
*/
private boolean removeEntry(EntryASDV<K, V> entry)
{
MapASDV.this.remove(entry.getKey());
return super.remove(entry);
}
/**
* Removes the key from the set. This method is meant to be called from
* out class. The overridden remove(Object o) of this inner class calls
* the remove of the out class, and the remove of the outer class calls
* remove(K o, boolean callFromOuterClass) instead of remove(Object o)
* to avoid Stack Overflow when remover of inner calls remove of outer
* and vice versa.
*
* @param o - the key
* @param callFromOuterClass - dummy variable.
* @return true if the key was removed from the Set
*/
public boolean remove(E o, boolean callFromOuterClass)
{
//remove key from shared keys set
return super.remove(o);
}
@Override
public Object clone()
{
return super.clone();
}
@Override
public void clear()
{
super.clear();
}
}
public MapASDV()
{
for (int i = 0; i < capacity; ++i)
{
map.add(new ListASDV<EntryASDV<K, V>>());
}
}
/**
* Double the size of the Map and rehashes the entries of the Map
*/
private void doubleTheSizeOfTheMapAndRehash()
{
capacity *= 2;
//>create a new arrayList of ListsASDV
ArrayList<ListASDV<EntryASDV<K, V>>> newMap = new ArrayList<ListASDV<EntryASDV<K, V>>>();
//>Add at every enetry of the arrayList a new ASDVList
for (int i = 0; i < capacity; ++i)
{
newMap.add(new ListASDV<EntryASDV<K, V>>());
}
//>for the size of the OLD arrayList
for (int i = 0; i < map.size(); ++i)//array list
{
//>> get The ASDVlist at i
ListASDV<EntryASDV<K, V>> list = map.get(i);
//>>for the size() of the ASDVlist
for (int j = 0; j < list.size(); ++j)
{
///>>>hash and put in the the new array
int index = hash( list.get(j).getKey().hashCode() );
newMap.get(index).add(list.get(j));
}
}
map = newMap;
}
class EntryASDV<K, V> implements Entry<K, V>, Comparable<K>
{
K key;
V value;
public EntryASDV(K key, V value)
{
this.key = key;
this.value = value;
}
@Override
public K getKey()
{
return key;
}
@Override
public V getValue()
{
return value;
}
@Override
public V setValue(V value)
{
V oldValue = this.value;
this.value = value;
return oldValue;
}
@Override
public String toString()
{
return "EntryASDV{" + "key=" + key + ", value=" + value + '}';
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final EntryASDV<?, ?> other = (EntryASDV<?, ?>) obj;
if (!Objects.equals(this.key, other.key))
{
return false;
}
return true;
}
/**
*
* @param o
* @return throws IllegalArgumentException if parameter class is not K
*/
@Override
public int compareTo(K o)
{
if (getClass() != o.getClass())
{
throw new IllegalArgumentException("ellegal parameter " + o);
}
return ((Comparable) key).compareTo(o);
}
}
@Override
public int size()
{
return this.keySet().size();
}
@Override
public boolean isEmpty()
{
throw new UnsupportedOperationException("Not supported....");
}
@Override
public boolean containsKey(Object key)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Return an entry of the map
*
* @param key the key of the entry to be returned
* @return the entry, or null if the key is not in the map
*/
private EntryASDV<K, V> getEntryForKey(Object key)
{
for (ListASDV<EntryASDV<K, V>> list : map)
{
Iterator<EntryASDV<K, V>> it = list.iterator();
while (it.hasNext())
{
EntryASDV<K, V> entry = it.next();
if (key.equals(entry.getKey()))
{
return entry;
}
}
}
return null;
}
/**
* Returns the index of the given key
*
* @param key a key of the map
* @return the index of a key in the map or -1, if the key is not in the map
*/
private int getIndexForKey(Object key)
{
for (int i = 0; i < map.size(); ++i)
{
Iterator<EntryASDV<K, V>> it = map.get(i).iterator();
while (it.hasNext())
{
EntryASDV<K, V> entry = it.next();
if (key.equals(entry.getKey()))
{
return i;
}
}
}
return -1;
}
/**
* Returns true if this map maps one or more keys to the specified value.
* More formally, returns true if and only if this map contains at least one
* mapping to a value v such that (value==null ? v==null : value.equals(v)).
* This operation will probably require time linear in the map size for most
* implementations of the Map interface.
*
* Parameters: value - value whose presence in this map is to be tested
* Returns: true if this map maps one or more keys to the specified value
* Throws: ClassCastException - if the value is of an inappropriate type for
* this map (optional) NullPointerException - if the specified value is null
* and this map does not permit null values (optional)
*
* @param value - value whose presence in this map is to be tested
* @return true if this map maps one or more keys to the specified value
* @throws NullPointerException - if the specified value is null and this
* map does not permit null values (optional)
*/
@Override
public boolean containsValue(Object value)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Returns the value to which the specified key is mapped, or null if this
* map contains no mapping for the key.
*
* More formally, if this map contains a mapping from a key k to a value v
* such that (key==null ? k==null : key.equals(k)), then this method returns
* v; otherwise it returns null. (There can be at most one such mapping.)
*
* If this map permits null values, then a return value of null does not
* necessarily indicate that the map contains no mapping for the key; it's
* also possible that the map explicitly maps the key to null. The
* containsKey operation may be used to distinguish these two cases.
*
* @param key - the key whose associated value is to be returned
* @return the value to which the specified key is mapped, or null if this
* map contains no mapping for the key
* @throws NullPointerException - if the specified key is null and this map
* does not permit null keys (optional)
*/
@Override
public V get(Object key)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Associates the specified value with the specified key in this map
* (optional operation). If the map previously contained a mapping for the
* key, the old value is replaced by the specified value. (A map m is said
* to contain a mapping for a key k if and only if m.containsKey(k) would
* return true.)
*
* @param key - key with which the specified value is to be associated
* @param value - value to be associated with the specified key
* @return the previous value associated with key, or null if there was no
* mapping for key. (A null return can also indicate that the map previously
* associated null with key, if the implementation supports null values.)
* @throws NullPointerException - if specified key or value is null and this
* map does not permit null keys
*/
@Override
public V put(K key, V value)
{
if (key == null || value == null)
{
throw new NullPointerException("parm(s) null");
}
//>if contains the key, replace the key's value
EntryASDV<K, V> entry = getEntryForKey(key);
if (entry != null)
{
V oldValue = entry.value;
entry.value = value;
return oldValue;
}
///>>hash and put in the array
int code = this.hash(key.hashCode());
int index = hash(code);
ListASDV<EntryASDV<K, V>> list = map.get(index);
EntryASDV e = new EntryASDV(key, value);
list.add(e);
//>>add the key to the shared keys-set
((SharedSet<K>) this.sharedKeySet).addEntry(key);
((SharedSet<Entry<K, V>>) this.sharedEntrySet).addEntry(e);
//>>get the value of this entry
V v = list.get(list.size() - 1).getValue();
//>> add value to the value collection
((SharedCollection<V>) this.sharedValuesCollection).addValue(v);
//>> if reach 75% capacity double the size
if ((double) this.size() / capacity >= 0.75)
{
this.doubleTheSizeOfTheMapAndRehash();
}
//>>return the value of Entry just added
return v;
}
int hash(int keyHashCode)
{
int h = hashHash(keyHashCode);
return Math.abs(h % capacity - 1);
}
/**
* Removes the first entry with the given values.
*
* @param value - the value to be removed
* @return true if removed, false otherwise
* @throws NullPointerException if the value is null
*/
private boolean removeFirstValue(Object value)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Ensure hash code is evenly distributed
*
* @param h - hash code
* @return evenly distributed hash code
*/
private static int hashHash(int h)
{
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
/**
* Removes the mapping for a key from this map if it is present (optional
* operation). More formally, if this map contains a mapping from key k to
* value v such that (key==null ? k==null : key.equals(k)), that mapping is
* removed. (The map can contain at most one such mapping.)
*
* @param key - key whose mapping is to be removed from the map
* @return the previous value associated with key, or null if there was no
* mapping for key.
* @throws NullPointerException - if the specified key is null and this map
* does not permit null keys
*/
@Override
public V remove(Object key)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Copies all of the mappings from the specified map to this map (optional
* operation). The effect of this call is equivalent to that of calling
* put(k, v) on this map once for each mapping from key k to value v in the
* specified map. The behavior of this operation is undefined if the
* specified map is modified while the operation is in progress.
*
* @param m - mappings to be stored in this map
* @throws NullPointerException - if the specified map is null, or if this
* map does not permit null keys or values, and the specified map contains
* null keys
*/
@Override
public void putAll(Map<? extends K, ? extends V> m)
{
if (m == null)
{
throw new NullPointerException("null parameter");
}
BiConsumer consumeEachEntry = new BiConsumer<K, V>()
{
MapASDV<K, V> mapForConsumer = MapASDV.this;
@Override
public void accept(K k, V v)
{
mapForConsumer.put(k, v);
}
};
m.forEach(consumeEachEntry);
}
/**
* Removes all of the mappings from this map (optional operation). The map
* will be empty after this call returns. Any shared sets are also cleared.
*/
@Override
public void clear()
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Returns a Set view of the keys contained in this map. The set is backed
* by the map, so changes to the map are reflected in the set, and
* vice-versa. If the map is modified while an iteration over the set is in
* progress (except through the iterator's own remove operation), the
* results of the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
* Iterator.remove, Set.remove, removeAll, retainAll, and clear operations.
* It does not support the add or addAll operations.
*
* @return a set view of the keys contained in this map
*/
@Override
public Set<K> keySet()
{
return this.sharedKeySet;
}
/**
* Returns a Collection view of the values contained in this map. The
* collection is backed by the map, so changes to the map are reflected in
* the collection, and vice-versa. If the map is modified while an iteration
* over the collection is in progress (except through the iterator's own
* remove operation), the results of the iteration are undefined. The
* collection supports element removal, which removes the corresponding
* mapping from the map, via the Iterator.remove, Collection.remove,
* removeAll, retainAll and clear operations. It does not support the add or
* addAll operations.
*
* @return
*/
@Override
public Collection<V> values()
{
return sharedValuesCollection;
}
/**
* Returns a Set view of the mappings contained in this map. The set is
* backed by the map, so changes to the map are reflected in the set, and
* vice-versa. If the map is modified while an iteration over the set is in
* progress (except through the iterator's own remove operation, or through
* the setValue operation on a map entry returned by the iterator) the
* results of the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
* Iterator.remove, Set.remove, removeAll, retainAll and clear operations.
* It does not support the add or addAll operations.
*
*
* @return a set view of the mappings contained in this map
*/
@Override
public Set<Entry<K, V>> entrySet()
{
return this.sharedEntrySet;
}
@Override
public String toString()
{
String s = "[ ";
for (int i = 0; i < capacity; ++i)
{
s += map.get(i).toString() + "\n";
}
s += " ]";
return s;
}
/**
* Created a deep copy of the MapASDV
*
* @return the deep copy of the map
*/
@Override
public Object clone()
{
/*
MapASDV<K, V> clonedMap = new MapASDV<K, V>();
//clonedMap.putAll(this);
for (ListASDV< EntryASDV<K, V>> list : this.map)
{
ListASDV< EntryASDV<K, V>> l = (ListASDV< EntryASDV<K, V>>) list.clone();
clonedMap.map.add(l);
}
return clonedMap;
*/
MapASDV<K, V> clonedMap = new MapASDV<K, V>();
clonedMap.putAll(this);
return clonedMap;
}
public static void main(String[] args)
{
MapASDV<String, Integer> map = new MapASDV();
System.out.println("---------------------------testing put(K, V)");
map.put("ann", 20);
map.put("coco", 25);
System.out.println(map);
mapASDV.MapASDV<String, Integer> clonedMap = ( mapASDV.MapASDV<String, Integer>) map.clone();
System.out.println("\n\n---------------------------testing double-the-size-and-rehash by reaching 0.75 load factor with another put(K, V)");
map.put("Jonathan", 30);
System.out.println(map);
map.put("Jonhathan", 45);
System.out.println(map);
map.put("Alexander", 33);
System.out.println(map);
System.out.println("\n\n---------------------------testing putAll(Map<K,V>)");
Map<String, Integer> anotherJavaMap = new HashMap();
anotherJavaMap.put("lion king", 45);
anotherJavaMap.put("HYENA", 6);
map.putAll(anotherJavaMap);
System.out.println(map);
System.out.println("\n\n---------------------------testing containsKey");
System.out.println(map.containsKey("Alexander"));
System.out.println(map.containsKey("alexander"));
System.out.println("\n\n---------------------------testing containsValue");
System.out.println(map.containsValue(33));
System.out.println(map.containsValue(34));
System.out.println("\n\n---------------------------testing getEntryForKey");
Entry<String, Integer> e = map.getEntryForKey("Alexander");
System.out.println( map.getEntryForKey("Alexander"));
System.out.println( map.getEntryForKey("Alex"));
System.out.println("\n\n---------------------------testing get");
System.out.println(map.get( "Alexander") );
System.out.println(map.get( "Alex") );
try{ map.get( null);}catch (NullPointerException ex ){System.out.println(ex.getMessage());}
System.out.println("\n\n---------------------------testing getIndexForKey");
System.out.println(map.getIndexForKey("Alexander"));
System.out.println(map.getIndexForKey("Alex"));
System.out.println("\n\n---------------------------testing isEmpty");
System.out.println( map.isEmpty());
System.out.println("\n\n---------------------------testing size");
System.out.println( map.size());
System.out.println( map);
System.out.println("\n\n---------------------------testing entrySet()");
Set<Entry<String, Integer>> entries = map.entrySet();
System.out.println( entries);
System.out.println("\n\n---------------------------testing keySet()");
Set<String> keys = map.keySet();
System.out.println( keys );
System.out.println("\n\n---------------------------testing values()");
Collection<Integer> values = map.values();
System.out.println( values);
System.out.println("\n\n---------------------------testing remove( K) coco 25");
map.remove("coco");
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing Entry-Collection remove ");
entries.remove( e);
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing Set Keys remove ");
keys.remove( "ann");
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing Set Values remove ");
values.remove( 45);
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing clear ");
map.clear();
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println("\n\n---------------------------testing add of sets and collections ");
try{ keys.add( "a");}catch (Exception ex ){System.out.println(ex.getMessage());}
try{ values.add( 33);}catch (Exception ex ){System.out.println(ex.getMessage());}
try{ entries.add( e);}catch (Exception ex ){System.out.println(ex.getMessage());}
System.out.println("\n\n---------------------------testing clone");
System.out.println( clonedMap);
System.out.println("---------------------------testing put(K, V) AGAIN");
map.put("Nicholas", 100);
map.put("a", 200);
map.put("b", -20);
System.out.println( map);
}
}

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.jdkPlatform>Graal_JDK_20</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>

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,233 @@
/*
* 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.Map.Entry;
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<Many1> 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) {
HashSet<Many2> verifyUnique = new HashSet<>();
List<Many2> returnList = new ArrayList<>();
// Check for exceptions
if (!childrenRight.equals(parentLeft.getClass())) {
//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;
}
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) {
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 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);
}
}

View File

@@ -0,0 +1,172 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Matrices.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.number {color: #6897bb}
.string {color: #6a8759}
.ST4 {color: #9876aa}
.ST5 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST3 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST2 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 4/Assignments/MP2_CalebFontenot/src/main/java/com/calebfontenot/mp2_calebfontenot/Matrices.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp2_calebfontenot;
<span class="literal">import</span> java.math.BigInteger;
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.concurrent.ForkJoinPool;
<span class="literal">import</span> java.util.concurrent.RecursiveTask;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> Matrices {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
Matrices.<span class="ST2">multiplyParallel</span>();
}
<span class="literal">public</span> <span class="literal">static</span> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="ST1">createRandomMatrix</span>(<span class="literal">int</span> rows, <span class="literal">int</span> columns) {
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; matrix = <span class="literal">new</span> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt;(rows);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; rows; ++i) {
ArrayList&lt;BigInteger&gt; row = <span class="literal">new</span> ArrayList&lt;BigInteger&gt;();
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; columns; ++j) {
row.add(<span class="literal">new</span> BigInteger(Integer.<span class="ST2">toString</span>(<span class="number">1</span> + (<span class="literal">int</span>) (Math.<span class="ST2">random</span>() * <span class="number">9</span>))));
}
matrix.add(row);
}
<span class="literal">return</span> matrix;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">multiplyParallel</span>() {
<span class="comment">/*</span>
<span class="comment"> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; A = new ArrayList&lt;&gt;();</span>
<span class="comment"> ArrayList&lt;BigInteger&gt; row1 = new ArrayList&lt;&gt;();</span>
<span class="comment"> row1.add(new BigInteger(&quot;6&quot;));</span>
<span class="comment"> row1.add(new BigInteger(&quot;1&quot;));</span>
<span class="comment"> ArrayList&lt;BigInteger&gt; row2 = new ArrayList&lt;&gt;();</span>
<span class="comment"> row2.add(new BigInteger(&quot;7&quot;));</span>
<span class="comment"> row2.add(new BigInteger(&quot;2&quot;));</span>
<span class="comment"> A.add(row1);</span>
<span class="comment"> A.add(row2);</span>
<span class="comment"> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; B = new ArrayList&lt;&gt;();</span>
<span class="comment"> row1 = new ArrayList&lt;BigInteger&gt;();</span>
<span class="comment"> row1.add(new BigInteger(&quot;4&quot;));</span>
<span class="comment"> row1.add(new BigInteger(&quot;1&quot;));</span>
<span class="comment"> row1.add(new BigInteger(&quot;2&quot;));</span>
<span class="comment"> row2 = new ArrayList&lt;BigInteger&gt;();</span>
<span class="comment"> row2.add(new BigInteger(&quot;1&quot;));</span>
<span class="comment"> row2.add(new BigInteger(&quot;9&quot;));</span>
<span class="comment"> row2.add(new BigInteger(&quot;5&quot;));</span>
<span class="comment"> B.add(row1);</span>
<span class="comment"> B.add(row2);</span>
<span class="comment"> */</span>
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; A = Matrices.<span class="ST2">createRandomMatrix</span>(<span class="number">2</span>, <span class="number">2</span>);
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; B = Matrices.<span class="ST2">createRandomMatrix</span>(<span class="number">2</span>, <span class="number">3</span>);
RecursiveTask&lt;ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt;&gt; rt
= <span class="literal">new</span> Matrices.MatricesMultiplication(<span class="number">0</span>, A.size() - <span class="number">1</span>, A, B);
ForkJoinPool pool = <span class="literal">new</span> ForkJoinPool();
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; mul = pool.invoke(rt);
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="string">MATRIX A</span><span class="string">&quot;</span>);
<span class="ST2">printMatrix</span>(A);
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">MATRIX B</span><span class="string">&quot;</span>);
<span class="ST2">printMatrix</span>(B);
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">MATRIX AxB</span><span class="string">&quot;</span>);
<span class="ST2">printMatrix</span>(mul);
}
<span class="literal">private</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printMatrix</span>(ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; matrix) {
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; matrix.size(); ++i) {
System.<span class="ST3">out</span>.println(matrix.get(i));
}
}
<span class="literal">static</span> <span class="literal">class</span> <span class="ST2">MatricesMultiplication</span> <span class="literal">extends</span> RecursiveTask&lt;ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt;&gt; {
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="ST4">A</span>;
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="ST4">B</span>;
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="comment">AxB</span>;
<span class="literal">final</span> <span class="literal">int</span> <span class="ST4">HOW_MANY_ROWS_IN_PARALLEL</span> = <span class="number">3</span>;<span class="comment">// threshold</span>
<span class="literal">int</span> <span class="ST4">startIndex</span>;
<span class="literal">int</span> <span class="ST4">endIndex</span>;
<span class="literal">public</span> MatricesMultiplication(<span class="literal">int</span> startIndex, <span class="literal">int</span> endIndex, ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; A, ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; B) {
<span class="literal">this</span>.<span class="ST4">startIndex</span> = startIndex;
<span class="literal">this</span>.<span class="ST4">endIndex</span> = endIndex;
<span class="literal">this</span>.<span class="ST4">A</span> = A;
<span class="literal">this</span>.<span class="ST4">B</span> = B;
}
@Override
<span class="literal">protected</span> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="ST5">compute</span>() {
<span class="comment">// Base case: if the number of rows in A is less than or equal to the threshold,</span>
<span class="comment">// perform matrix multiplication sequentially</span>
<span class="literal">if</span> (<span class="ST4">endIndex</span> - <span class="ST4">startIndex</span> + <span class="number">1</span> &lt;= <span class="ST4">HOW_MANY_ROWS_IN_PARALLEL</span>) {
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; result = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="ST4">startIndex</span>; i &lt;= <span class="ST4">endIndex</span>; i++) {
ArrayList&lt;BigInteger&gt; rowResult = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; <span class="ST4">B</span>.get(<span class="number">0</span>).size(); j++) {
BigInteger sum = BigInteger.<span class="ST3">ZERO</span>;
<span class="literal">for</span> (<span class="literal">int</span> k = <span class="number">0</span>; k &lt; <span class="ST4">A</span>.get(<span class="number">0</span>).size(); k++) {
sum = sum.add(<span class="ST4">A</span>.get(i).get(k).multiply(<span class="ST4">B</span>.get(k).get(j)));
}
rowResult.add(sum);
}
result.add(rowResult);
}
<span class="literal">return</span> result;
} <span class="literal">else</span> {
<span class="comment">// Split the task into smaller subtasks</span>
<span class="literal">int</span> middle = (<span class="ST4">startIndex</span> + <span class="ST4">endIndex</span>) / <span class="number">2</span>;
<span class="ST2">MatricesMultiplication</span> leftTask = <span class="literal">new</span> MatricesMultiplication(<span class="ST4">startIndex</span>, middle, <span class="ST4">A</span>, <span class="ST4">B</span>);
<span class="ST2">MatricesMultiplication</span> rightTask = <span class="literal">new</span> MatricesMultiplication(middle + <span class="number">1</span>, <span class="ST4">endIndex</span>, <span class="ST4">A</span>, <span class="ST4">B</span>);
<span class="comment">// Fork the subtasks</span>
leftTask.fork();
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; rightResult = rightTask.compute();
<span class="comment">// Join the results of the subtasks</span>
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; leftResult = leftTask.join();
<span class="comment">// Merge the results</span>
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; result = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; leftResult.size(); i++) {
result.add(<span class="literal">new</span> ArrayList&lt;&gt;(leftResult.get(i)));
}
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; rightResult.size(); i++) {
result.add(<span class="literal">new</span> ArrayList&lt;&gt;(rightResult.get(i)));
}
<span class="literal">return</span> result;
}
}
}
}
</pre></body>
</html>

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>com.calebfontenot</groupId>
<artifactId>MP2_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<exec.mainClass>com.calebfontenot.mp2_calebfontenot.MP2_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.calebfontenot.mp2_calebfontenot;
/**
*
* @author caleb
*/
public class MP2_CalebFontenot {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

View File

@@ -0,0 +1,142 @@
/*
* 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 com.calebfontenot.mp2_calebfontenot;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
/**
*
* @author caleb
*/
public class Matrices {
public static void main(String[] args) {
Matrices.multiplyParallel();
}
public static ArrayList<ArrayList<BigInteger>> createRandomMatrix(int rows, int columns) {
ArrayList<ArrayList<BigInteger>> matrix = new ArrayList<ArrayList<BigInteger>>(rows);
for (int i = 0; i < rows; ++i) {
ArrayList<BigInteger> row = new ArrayList<BigInteger>();
for (int j = 0; j < columns; ++j) {
row.add(new BigInteger(Integer.toString(1 + (int) (Math.random() * 9))));
}
matrix.add(row);
}
return matrix;
}
public static void multiplyParallel() {
/*
ArrayList<ArrayList<BigInteger>> A = new ArrayList<>();
ArrayList<BigInteger> row1 = new ArrayList<>();
row1.add(new BigInteger("6"));
row1.add(new BigInteger("1"));
ArrayList<BigInteger> row2 = new ArrayList<>();
row2.add(new BigInteger("7"));
row2.add(new BigInteger("2"));
A.add(row1);
A.add(row2);
ArrayList<ArrayList<BigInteger>> B = new ArrayList<>();
row1 = new ArrayList<BigInteger>();
row1.add(new BigInteger("4"));
row1.add(new BigInteger("1"));
row1.add(new BigInteger("2"));
row2 = new ArrayList<BigInteger>();
row2.add(new BigInteger("1"));
row2.add(new BigInteger("9"));
row2.add(new BigInteger("5"));
B.add(row1);
B.add(row2);
*/
ArrayList<ArrayList<BigInteger>> A = Matrices.createRandomMatrix(2, 2);
ArrayList<ArrayList<BigInteger>> B = Matrices.createRandomMatrix(2, 3);
RecursiveTask<ArrayList<ArrayList<BigInteger>>> rt
= new Matrices.MatricesMultiplication(0, A.size() - 1, A, B);
ForkJoinPool pool = new ForkJoinPool();
ArrayList<ArrayList<BigInteger>> mul = pool.invoke(rt);
System.out.println("MATRIX A");
printMatrix(A);
System.out.println("\nMATRIX B");
printMatrix(B);
System.out.println("\nMATRIX AxB");
printMatrix(mul);
}
private static void printMatrix(ArrayList<ArrayList<BigInteger>> matrix) {
for (int i = 0; i < matrix.size(); ++i) {
System.out.println(matrix.get(i));
}
}
static class MatricesMultiplication extends RecursiveTask<ArrayList<ArrayList<BigInteger>>> {
ArrayList<ArrayList<BigInteger>> A;
ArrayList<ArrayList<BigInteger>> B;
ArrayList<ArrayList<BigInteger>> AxB;
final int HOW_MANY_ROWS_IN_PARALLEL = 3;// threshold
int startIndex;
int endIndex;
public MatricesMultiplication(int startIndex, int endIndex, ArrayList<ArrayList<BigInteger>> A, ArrayList<ArrayList<BigInteger>> B) {
this.startIndex = startIndex;
this.endIndex = endIndex;
this.A = A;
this.B = B;
}
@Override
protected ArrayList<ArrayList<BigInteger>> compute() {
// Base case: if the number of rows in A is less than or equal to the threshold,
// perform matrix multiplication sequentially
if (endIndex - startIndex + 1 <= HOW_MANY_ROWS_IN_PARALLEL) {
ArrayList<ArrayList<BigInteger>> result = new ArrayList<>();
for (int i = startIndex; i <= endIndex; i++) {
ArrayList<BigInteger> rowResult = new ArrayList<>();
for (int j = 0; j < B.get(0).size(); j++) {
BigInteger sum = BigInteger.ZERO;
for (int k = 0; k < A.get(0).size(); k++) {
sum = sum.add(A.get(i).get(k).multiply(B.get(k).get(j)));
}
rowResult.add(sum);
}
result.add(rowResult);
}
return result;
} else {
// Split the task into smaller subtasks
int middle = (startIndex + endIndex) / 2;
MatricesMultiplication leftTask = new MatricesMultiplication(startIndex, middle, A, B);
MatricesMultiplication rightTask = new MatricesMultiplication(middle + 1, endIndex, A, B);
// Fork the subtasks
leftTask.fork();
ArrayList<ArrayList<BigInteger>> rightResult = rightTask.compute();
// Join the results of the subtasks
ArrayList<ArrayList<BigInteger>> leftResult = leftTask.join();
// Merge the results
ArrayList<ArrayList<BigInteger>> result = new ArrayList<>();
for (int i = 0; i < leftResult.size(); i++) {
result.add(new ArrayList<>(leftResult.get(i)));
}
for (int i = 0; i < rightResult.size(); i++) {
result.add(new ArrayList<>(rightResult.get(i)));
}
return result;
}
}
}
}

View File

@@ -0,0 +1,813 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>MapASDV.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.number {color: #6897bb}
.string {color: #6a8759}
.ST1 {color: #9876aa}
.ST3 {color: #ffc66d}
.ST4 {color: #8a653b}
.comment {color: #808080}
.whitespace {color: #505050}
.ST5 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST6 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST2 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 4/Assignments/MapASDV_CalebFontenot/src/mapasdv_calebfontenot/MapASDV.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> mapasdv_calebfontenot;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="comment">//import ListASDV;</span>
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.Collection;
<span class="literal">import</span> java.util.HashMap;
<span class="literal">import</span> java.util.HashSet;
<span class="literal">import</span> java.util.Iterator;
<span class="literal">import</span> java.util.Map;
<span class="literal">import</span> java.util.Objects;
<span class="literal">import</span> java.util.Set;
<span class="literal">import</span> java.util.function.BiConsumer;<span class="comment">//needed in putAll</span>
<span class="literal">public</span> <span class="literal">class</span> MapASDV&lt;K, V&gt; <span class="literal">implements</span> Map&lt;K, V&gt;, Cloneable {
<span class="literal">private</span> <span class="literal">int</span> <span class="ST1">capacity</span> = <span class="number">4</span>;
<span class="literal">private</span> <span class="literal">double</span> <span class="comment">loadFactor</span> = <span class="number">0.75</span>;
<span class="literal">private</span> ArrayList&lt;ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;&gt; <span class="ST1">map</span> = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">private</span> Set&lt;K&gt; <span class="ST1">sharedKeySet</span> = <span class="literal">new</span> SharedSet&lt;&gt;();
<span class="literal">private</span> Set&lt;<span class="ST2">Entry</span>&lt;K, V&gt;&gt; <span class="ST1">sharedEntrySet</span> = <span class="literal">new</span> SharedSet&lt;&gt;();
<span class="literal">private</span> Collection&lt;V&gt; <span class="ST1">sharedValuesCollection</span> = <span class="literal">new</span> SharedCollection&lt;&gt;();
<span class="literal">private</span> <span class="literal">class</span> SharedCollection&lt;V&gt; <span class="literal">extends</span> ArrayList&lt;V&gt; {
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">addValue</span>(V v) {
<span class="literal">return</span> <span class="literal">this</span>.add(v);
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">remove</span>(Object o) {
<span class="comment">//&gt; The parameter is key not entry if we are here</span>
<span class="comment">//&gt;&gt;remove value) and key) from the map</span>
<span class="literal">return</span> MapASDV.<span class="literal">this</span>.removeFirstValue(o);
<span class="comment">//&gt;&gt;remove key from shared values set</span>
<span class="comment">//return super.remove(o);</span>
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">one</span> <span class="comment">value</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">collection</span><span class="comment">.</span> <span class="comment">This</span> <span class="comment">method</span> <span class="comment">is</span> <span class="comment">meant</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">called</span> <span class="comment">from</span> <span class="comment">out</span> <span class="comment">class</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">overridden</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">Object</span> <span class="comment">o</span><span class="comment">) </span><span class="comment">of</span> <span class="comment">this</span> <span class="comment">inner</span> <span class="comment">class</span><span class="comment">, </span><span class="comment">calls</span> <span class="comment">the</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">outer</span> <span class="comment">class</span><span class="comment">(</span><span class="comment">MapASDV</span><span class="comment">), </span><span class="comment">and</span> <span class="comment">the</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">outer</span> <span class="comment">class</span><span class="comment">, </span><span class="comment">calls</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">V</span> <span class="comment">v</span><span class="comment">, </span><span class="comment">boolean</span> <span class="comment">callFromOuterClass</span><span class="comment">) </span><span class="comment">instead</span> <span class="comment">of</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">Object</span> <span class="comment">o</span><span class="comment">) </span><span class="comment">to</span> <span class="comment">avoid</span> <span class="comment">Stack</span> <span class="comment">Overflow</span> <span class="comment">when</span> <span class="comment">remover</span> <span class="comment">of</span> <span class="comment">inner</span> <span class="comment">calls</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">outer</span> <span class="comment">and</span> <span class="comment">vice</span> <span class="comment">versa</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">o</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">key</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">callFromOuterClass</span><span class="comment"> - </span><span class="comment">dummy</span> <span class="comment">variable</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">was</span> <span class="comment">removed</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">Set</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">remove</span>(V v, <span class="literal">boolean</span> callFromOuterClass) {
<span class="comment">//remove key from shared keys set</span>
<span class="literal">boolean</span> b = <span class="literal">super</span>.remove(v);
<span class="literal">return</span> b;
}
@Override
<span class="literal">public</span> Object <span class="ST3">clone</span>() {
<span class="literal">return</span> <span class="literal">super</span>.clone();
}
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">clear</span>() {
<span class="literal">super</span>.clear();
}
}
<span class="literal">private</span> <span class="literal">class</span> SharedSet&lt;E&gt; <span class="literal">extends</span> HashSet&lt;E&gt; {
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">add</span>(E e) {
<span class="literal">return</span> <span class="literal">super</span>.add(e);
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">removeAll</span>(Collection&lt;?&gt; c) {
<span class="literal">return</span> <span class="literal">super</span>.removeAll(c);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Adds</span> <span class="comment">an</span> <span class="comment">EntryASDV</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">set</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">is</span> <span class="comment">private</span> <span class="comment">and</span> <span class="comment">cannot</span> <span class="comment">be</span> <span class="comment">used</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">user</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">Set</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">is</span> <span class="comment">used</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">MapASDV</span> <span class="comment">to</span> <span class="comment">add</span> <span class="comment">EntriesASDV</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">SharedSet</span><span class="comment">.</span> <span class="comment">Without</span> <span class="comment">this</span> <span class="comment">method</span> <span class="comment">we</span> <span class="comment">wouldn</span><span class="comment">&#39;</span><span class="comment">t</span> <span class="comment">be</span> <span class="comment">able</span> <span class="comment">to</span> <span class="comment">create</span> <span class="comment">a</span> <span class="comment">Set</span> <span class="comment">of</span> <span class="comment">keys</span> <span class="comment">or</span> <span class="comment">a</span> <span class="comment">Set</span> <span class="comment">of</span> <span class="comment">entries</span> <span class="comment">to</span> <span class="comment">give</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">user</span> <span class="comment">via</span> <span class="comment">methods</span> <span class="comment">keySet</span><span class="comment">() </span><span class="comment">and</span> <span class="comment">entrySet</span><span class="comment">() </span><span class="comment">of</span> <span class="comment">this</span> <span class="comment">Map</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">e</span> <span class="comment">EntryASDV</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">entry</span> <span class="comment">was</span> <span class="comment">added</span> <span class="comment">false</span> <span class="comment">otherwise</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">boolean</span> <span class="ST3">addEntry</span>(E e) {
<span class="literal">return</span> <span class="literal">super</span>.add(e);
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">remove</span>(Object o) {
<span class="comment">//&gt;if parameter oo is EntryASDV call auxiliary method removeEntry</span>
<span class="literal">if</span> (o <span class="literal">instanceof</span> EntryASDV) {
<span class="literal">return</span> removeEntry((EntryASDV) o);
}
<span class="comment">//&gt; The parameter is key not entry if we are here</span>
<span class="comment">//&gt;&gt;remove key from the map</span>
MapASDV.<span class="literal">this</span>.remove(o);
<span class="comment">//&gt;&gt;remove key from shared keys set</span>
<span class="literal">return</span> <span class="literal">super</span>.remove(o);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">the</span> <span class="comment">entry</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">shared</span> <span class="comment">set</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">entry</span> <span class="comment">the</span> <span class="comment">entry</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">removed</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">entry</span> <span class="comment">was</span> <span class="comment">removed</span><span class="comment">, </span><span class="comment">false</span> <span class="comment">otherwise</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">boolean</span> <span class="ST3">removeEntry</span>(EntryASDV&lt;K, V&gt; entry) {
MapASDV.<span class="literal">this</span>.remove(entry.getKey());
<span class="literal">return</span> <span class="literal">super</span>.remove(entry);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">set</span><span class="comment">.</span> <span class="comment">This</span> <span class="comment">method</span> <span class="comment">is</span> <span class="comment">meant</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">called</span> <span class="comment">from</span> <span class="comment">out</span> <span class="comment">class</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">overridden</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">Object</span> <span class="comment">o</span><span class="comment">) </span><span class="comment">of</span> <span class="comment">this</span> <span class="comment">inner</span> <span class="comment">class</span> <span class="comment">calls</span> <span class="comment">the</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">out</span> <span class="comment">class</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">the</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">outer</span> <span class="comment">class</span> <span class="comment">calls</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">K</span> <span class="comment">o</span><span class="comment">, </span><span class="comment">boolean</span> <span class="comment">callFromOuterClass</span><span class="comment">) </span><span class="comment">instead</span> <span class="comment">of</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">Object</span> <span class="comment">o</span><span class="comment">) </span><span class="comment">to</span> <span class="comment">avoid</span> <span class="comment">Stack</span> <span class="comment">Overflow</span> <span class="comment">when</span> <span class="comment">remover</span> <span class="comment">of</span> <span class="comment">inner</span> <span class="comment">calls</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">outer</span> <span class="comment">and</span> <span class="comment">vice</span> <span class="comment">versa</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">o</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">key</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">callFromOuterClass</span><span class="comment"> - </span><span class="comment">dummy</span> <span class="comment">variable</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">was</span> <span class="comment">removed</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">Set</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">remove</span>(E o, <span class="literal">boolean</span> callFromOuterClass) {
<span class="comment">//remove key from shared keys set</span>
<span class="literal">return</span> <span class="literal">super</span>.remove(o);
}
@Override
<span class="literal">public</span> Object <span class="ST3">clone</span>() {
<span class="literal">return</span> <span class="literal">super</span>.clone();
}
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">clear</span>() {
<span class="literal">super</span>.clear();
}
}
<span class="literal">public</span> MapASDV() {
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">capacity</span>; ++i) {
<span class="ST1">map</span>.add(<span class="literal">new</span> ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;());
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Double</span> <span class="comment">the</span> <span class="comment">size</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">Map</span> <span class="comment">and</span> <span class="comment">rehashes</span> <span class="comment">the</span> <span class="comment">entries</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">Map</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">void</span> <span class="ST3">doubleTheSizeOfTheMapAndRehash</span>() {
<span class="ST1">capacity</span> *= <span class="number">2</span>;
<span class="comment">//&gt;create a new arrayList of ListsASDV</span>
ArrayList&lt;ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;&gt; newMap = <span class="literal">new</span> ArrayList&lt;ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;&gt;();
<span class="comment">//&gt;Add at every enetry of the arrayList a new ASDVList</span>
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">capacity</span>; ++i) {
newMap.add(<span class="literal">new</span> ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;());
}
<span class="comment">//&gt;for the size of the OLD arrayList</span>
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i)<span class="comment">//array list</span>
{
<span class="comment">//&gt;&gt; get The ASDVlist at i</span>
ListASDV&lt;EntryASDV&lt;K, V&gt;&gt; list = <span class="ST1">map</span>.get(i);
<span class="comment">//&gt;&gt;for the size() of the ASDVlist</span>
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; list.size(); ++j) {
<span class="comment">///&gt;&gt;&gt;hash and put in the the new array </span>
<span class="literal">int</span> index = hash(list.get(j).getKey().hashCode());
newMap.get(index).add(list.get(j));
}
}
<span class="ST1">map</span> = newMap;
}
<span class="literal">class</span> EntryASDV&lt;K, V&gt; <span class="literal">implements</span> <span class="ST2">Entry</span>&lt;K, V&gt;, Comparable&lt;K&gt; {
K <span class="ST1">key</span>;
V <span class="ST1">value</span>;
<span class="literal">public</span> EntryASDV(K key, V value) {
<span class="literal">this</span>.<span class="ST1">key</span> = key;
<span class="literal">this</span>.<span class="ST1">value</span> = value;
}
@Override
<span class="literal">public</span> K <span class="ST3">getKey</span>() {
<span class="literal">return</span> <span class="ST1">key</span>;
}
@Override
<span class="literal">public</span> V <span class="ST3">getValue</span>() {
<span class="literal">return</span> <span class="ST1">value</span>;
}
@Override
<span class="literal">public</span> V <span class="ST3">setValue</span>(V value) {
V oldValue = <span class="literal">this</span>.<span class="ST1">value</span>;
<span class="literal">this</span>.<span class="ST1">value</span> = value;
<span class="literal">return</span> oldValue;
}
@Override
<span class="literal">public</span> String <span class="ST3">toString</span>() {
<span class="literal">return</span> <span class="string">&quot;</span><span class="string">EntryASDV{</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">key=</span><span class="string">&quot;</span> + <span class="ST1">key</span> + <span class="string">&quot;</span><span class="string">, value=</span><span class="string">&quot;</span> + <span class="ST1">value</span> + <span class="string">&#39;</span><span class="string">}</span><span class="string">&#39;</span>;
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">equals</span>(Object obj) {
<span class="literal">if</span> (<span class="literal">this</span> == obj) {
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">if</span> (obj == <span class="literal">null</span>) {
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="literal">if</span> (getClass() != obj.getClass()) {
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="literal">final</span> EntryASDV&lt;?, ?&gt; other = (EntryASDV&lt;?, ?&gt;) obj;
<span class="literal">if</span> (!Objects.<span class="ST2">equals</span>(<span class="literal">t</span><span class="literal">his</span>.<span class="ST1">key</span>, other.<span class="ST1">key</span>)) {
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">o</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">throws</span> <span class="comment">IllegalArgumentException</span> <span class="comment">if</span> <span class="comment">parameter</span> <span class="comment">class</span> <span class="comment">is</span> <span class="comment">not</span> <span class="comment">K</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">compareTo</span>(K o) {
<span class="literal">if</span> (getClass() != o.getClass()) {
<span class="literal">throw</span> <span class="literal">new</span> IllegalArgumentException(<span class="string">&quot;</span><span class="string">ellegal parameter </span><span class="string">&quot;</span> + o);
}
<span class="literal">return</span> ((Comparable) <span class="ST1">key</span>).compareTo(o);
}
}
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">size</span>() {
<span class="literal">return</span> <span class="literal">this</span>.keySet().size();
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">isEmpty</span>() {
<span class="literal">if</span> (<span class="ST1">map</span>.size() == <span class="number">0</span>) {
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="comment">// done</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">containsKey</span>(Object key) {
String dashes = <span class="string">&quot;</span><span class="string">---------</span><span class="string">&quot;</span>;
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> containsKey(</span><span class="string">&quot;</span> + key.toString() + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; <span class="ST1">map</span>.get(i).size(); ++j) {
<span class="literal">if</span> (<span class="ST1">map</span>.get(i).get(j).<span class="ST1">key</span>.equals(key)) {
<span class="literal">return</span> <span class="literal">true</span>;
}
}
}
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Return</span> <span class="comment">an</span> <span class="comment">entry</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">key</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">entry</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">returned</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">entry</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">not</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment">*/</span>
<span class="literal">private</span> EntryASDV&lt;K, V&gt; <span class="ST3">getEntryForKey</span>(Object key) {
<span class="literal">for</span> (ListASDV&lt;EntryASDV&lt;K, V&gt;&gt; list : <span class="ST1">map</span>) {
Iterator&lt;EntryASDV&lt;K, V&gt;&gt; it = list.iterator();
<span class="literal">while</span> (it.hasNext()) {
EntryASDV&lt;K, V&gt; entry = it.next();
<span class="literal">if</span> (key.equals(entry.getKey())) {
<span class="literal">return</span> entry;
}
}
}
<span class="literal">return</span> <span class="literal">null</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">the</span> <span class="comment">index</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">given</span> <span class="comment">key</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">key</span> <span class="comment">a</span> <span class="comment">key</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">index</span> <span class="comment">of</span> <span class="comment">a</span> <span class="comment">key</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">or</span><span class="comment"> -1, </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">not</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">int</span> <span class="ST3">getIndexForKey</span>(Object key) {
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
Iterator&lt;EntryASDV&lt;K, V&gt;&gt; it = <span class="ST1">map</span>.get(i).iterator();
<span class="literal">while</span> (it.hasNext()) {
EntryASDV&lt;K, V&gt; entry = it.next();
<span class="literal">if</span> (key.equals(entry.getKey())) {
<span class="literal">return</span> i;
}
}
}
<span class="literal">return</span> -<span class="number">1</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">maps</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">keys</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span><span class="comment">.</span> <span class="comment">More</span> <span class="comment">formally</span><span class="comment">, </span><span class="comment">returns</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">and</span> <span class="comment">only</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">at</span> <span class="comment">least</span> <span class="comment">one</span> <span class="comment">mapping</span> <span class="comment">to</span> <span class="comment">a</span> <span class="comment">value</span> <span class="comment">v</span> <span class="comment">such</span> <span class="comment">that</span><span class="comment"> (</span><span class="comment">value</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> ? </span><span class="comment">v</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> : </span><span class="comment">value</span><span class="comment">.</span><span class="comment">equals</span><span class="comment">(</span><span class="comment">v</span><span class="comment">))</span><span class="comment">.</span> <span class="comment">This</span> <span class="comment">operation</span> <span class="comment">will</span> <span class="comment">probably</span> <span class="comment">require</span> <span class="comment">time</span> <span class="comment">linear</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">size</span> <span class="comment">for</span> <span class="comment">most</span> <span class="comment">implementations</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">Map</span> <span class="comment">interface</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">Parameters</span><span class="comment">: </span><span class="comment">value</span><span class="comment"> - </span><span class="comment">value</span> <span class="comment">whose</span> <span class="comment">presence</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">tested</span> <span class="comment">Returns</span><span class="comment">: </span><span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">maps</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">keys</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span> <span class="comment">Throws</span><span class="comment">: </span><span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">of</span> <span class="comment">an</span> <span class="comment">inappropriate</span> <span class="comment">type</span> <span class="comment">for</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment"> (</span><span class="comment">optional</span><span class="comment">) </span><span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">values</span><span class="comment"> (</span><span class="comment">optional</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">value</span><span class="comment"> - </span><span class="comment">value</span> <span class="comment">whose</span> <span class="comment">presence</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">tested</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">maps</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">keys</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">values</span><span class="comment"> (</span><span class="comment">optional</span><span class="comment">)</span>
<span class="comment">*/</span>
<span class="comment">//done</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">containsValue</span>(Object value) {
String dashes = <span class="string">&quot;</span><span class="string">---------</span><span class="string">&quot;</span>;
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> containsValue(</span><span class="string">&quot;</span> + value.toString() + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; <span class="ST1">map</span>.get(i).size(); ++j) {
<span class="literal">if</span> (<span class="ST1">map</span>.get(i).get(j).<span class="ST1">value</span>.equals(value)) {
<span class="literal">return</span> <span class="literal">true</span>;
}
}
}
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">to</span> <span class="comment">which</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">mapped</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">no</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">key</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">More</span> <span class="comment">formally</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">a</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">a</span> <span class="comment">key</span> <span class="comment">k</span> <span class="comment">to</span> <span class="comment">a</span> <span class="comment">value</span> <span class="comment">v</span> <span class="comment">such</span> <span class="comment">that</span><span class="comment"> (</span><span class="comment">key</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> ? </span><span class="comment">k</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> : </span><span class="comment">key</span><span class="comment">.</span><span class="comment">equals</span><span class="comment">(</span><span class="comment">k</span><span class="comment">)), </span><span class="comment">then</span> <span class="comment">this</span> <span class="comment">method</span> <span class="comment">returns</span> <span class="comment">v</span><span class="comment">; </span><span class="comment">otherwise</span> <span class="comment">it</span> <span class="comment">returns</span> <span class="comment">null</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">There</span> <span class="comment">can</span> <span class="comment">be</span> <span class="comment">at</span> <span class="comment">most</span> <span class="comment">one</span> <span class="comment">such</span> <span class="comment">mapping</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">If</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">permits</span> <span class="comment">null</span> <span class="comment">values</span><span class="comment">, </span><span class="comment">then</span> <span class="comment">a</span> <span class="comment">return</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">null</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">necessarily</span> <span class="comment">indicate</span> <span class="comment">that</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">no</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">key</span><span class="comment">; </span><span class="comment">it</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">also</span> <span class="comment">possible</span> <span class="comment">that</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">explicitly</span> <span class="comment">maps</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">to</span> <span class="comment">null</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">containsKey</span> <span class="comment">operation</span> <span class="comment">may</span> <span class="comment">be</span> <span class="comment">used</span> <span class="comment">to</span> <span class="comment">distinguish</span> <span class="comment">these</span> <span class="comment">two</span> <span class="comment">cases</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">key</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">key</span> <span class="comment">whose</span> <span class="comment">associated</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">returned</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">to</span> <span class="comment">which</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">mapped</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">no</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">key</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">keys</span><span class="comment"> (</span><span class="comment">optional</span><span class="comment">)</span>
<span class="comment">*/</span>
<span class="comment">//done</span>
@Override
<span class="literal">public</span> V <span class="ST3">get</span>(Object key) {
String dashes = <span class="string">&quot;</span><span class="string">---------</span><span class="string">&quot;</span>;
<span class="literal">try</span> {
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> get(</span><span class="string">&quot;</span> + key.toString() + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
} <span class="literal">catch</span> (NullPointerException ex) {
<span class="comment">//System.out.println(ex);</span>
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> get(</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">null</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">null parameter</span><span class="string">&quot;</span>);
}
Object currentKey = <span class="literal">null</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; <span class="ST1">map</span>.get(i).size(); ++j) {
currentKey = <span class="ST1">map</span>.get(i).get(j).<span class="ST1">key</span>;
<span class="literal">if</span> (currentKey.equals(key)) {
<span class="literal">return</span> <span class="ST1">map</span>.get(i).get(j).<span class="ST1">value</span>;
}
}
}
<span class="literal">return</span> <span class="literal">null</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Associates</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">previously</span> <span class="comment">contained</span> <span class="comment">a</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">key</span><span class="comment">, </span><span class="comment">the</span> <span class="comment">old</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">replaced</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">A</span> <span class="comment">map</span> <span class="comment">m</span> <span class="comment">is</span> <span class="comment">said</span> <span class="comment">to</span> <span class="comment">contain</span> <span class="comment">a</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">a</span> <span class="comment">key</span> <span class="comment">k</span> <span class="comment">if</span> <span class="comment">and</span> <span class="comment">only</span> <span class="comment">if</span> <span class="comment">m</span><span class="comment">.</span><span class="comment">containsKey</span><span class="comment">(</span><span class="comment">k</span><span class="comment">) </span><span class="comment">would</span> <span class="comment">return</span> <span class="comment">true</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">key</span><span class="comment"> - </span><span class="comment">key</span> <span class="comment">with</span> <span class="comment">which</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">associated</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">value</span><span class="comment"> - </span><span class="comment">value</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">associated</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">previous</span> <span class="comment">value</span> <span class="comment">associated</span> <span class="comment">with</span> <span class="comment">key</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">there</span> <span class="comment">was</span> <span class="comment">no</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">key</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">A</span> <span class="comment">null</span> <span class="comment">return</span> <span class="comment">can</span> <span class="comment">also</span> <span class="comment">indicate</span> <span class="comment">that</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">previously</span> <span class="comment">associated</span> <span class="comment">null</span> <span class="comment">with</span> <span class="comment">key</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">implementation</span> <span class="comment">supports</span> <span class="comment">null</span> <span class="comment">values</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">or</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">keys</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> V <span class="ST3">put</span>(K key, V value) {
<span class="literal">if</span> (key == <span class="literal">null</span> || value == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">parm(s) null</span><span class="string">&quot;</span>);
}
<span class="comment">//&gt;if contains the key, replace the key&#39;s value </span>
EntryASDV&lt;K, V&gt; entry = getEntryForKey(key);
<span class="literal">if</span> (entry != <span class="literal">null</span>) {
V oldValue = entry.<span class="ST1">value</span>;
entry.<span class="ST1">value</span> = value;
<span class="literal">return</span> oldValue;
}
<span class="comment">///&gt;&gt;hash and put in the array </span>
<span class="literal">int</span> code = <span class="literal">this</span>.hash(key.hashCode());
<span class="literal">int</span> index = hash(code);
ListASDV&lt;EntryASDV&lt;K, V&gt;&gt; list = <span class="ST1">map</span>.get(index);
EntryASDV e = <span class="literal">new</span> EntryASDV(key, value);
list.add(e);
<span class="comment">//&gt;&gt;add the key to the shared keys-set</span>
((SharedSet&lt;K&gt;) <span class="literal">this</span>.<span class="ST1">sharedKeySet</span>).addEntry(key);
((SharedSet&lt;<span class="ST2">Entry</span>&lt;K, V&gt;&gt;) <span class="literal">this</span>.<span class="ST1">sharedEntrySet</span>).addEntry(e);
<span class="comment">//&gt;&gt;get the value of this entry</span>
V v = list.get(list.size() - <span class="number">1</span>).getValue();
<span class="comment">//&gt;&gt; add value to the value collection</span>
((SharedCollection&lt;V&gt;) <span class="literal">this</span>.<span class="ST1">sharedValuesCollection</span>).addValue(v);
<span class="comment">//&gt;&gt; if reach 75% capacity double the size</span>
<span class="literal">if</span> ((<span class="literal">double</span>) <span class="literal">this</span>.size() / <span class="ST1">capacity</span> &gt;= <span class="number">0.75</span>) {
<span class="literal">this</span>.doubleTheSizeOfTheMapAndRehash();
}
<span class="comment">//&gt;&gt;return the value of Entry just added</span>
<span class="literal">return</span> v;
}
<span class="literal">int</span> <span class="ST3">hash</span>(<span class="literal">int</span> keyHashCode) {
<span class="literal">int</span> h = <span class="ST2">hashHash</span>(keyHashCode);
<span class="literal">return</span> Math.<span class="ST2">abs</span>(h % <span class="ST1">capacity</span> - <span class="number">1</span>);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">the</span> <span class="comment">first</span> <span class="comment">entry</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">given</span> <span class="comment">values</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">value</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">value</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">removed</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">removed</span><span class="comment">, </span><span class="comment">false</span> <span class="comment">otherwise</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">null</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">boolean</span> <span class="ST3">removeFirstValue</span>(Object value) {
Iterator&lt;V&gt; iterator = <span class="ST1">sharedValuesCollection</span>.iterator();
<span class="literal">while</span> (iterator.hasNext()) {
Object o = iterator.next();
<span class="literal">if</span> (o.equals(value)) {
iterator.remove();
<span class="literal">return</span> <span class="literal">true</span>;
}
}
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Ensure</span> <span class="comment">hash</span> <span class="comment">code</span> <span class="comment">is</span> <span class="comment">evenly</span> <span class="comment">distributed</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">h</span><span class="comment"> - </span><span class="comment">hash</span> <span class="comment">code</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">evenly</span> <span class="comment">distributed</span> <span class="comment">hash</span> <span class="comment">code</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST6">hashHash</span>(<span class="literal">int</span> h) {
h ^= (h &gt;&gt;&gt; <span class="number">20</span>) ^ (h &gt;&gt;&gt; <span class="number">12</span>);
<span class="literal">return</span> h ^ (h &gt;&gt;&gt; <span class="number">7</span>) ^ (h &gt;&gt;&gt; <span class="number">4</span>);
}
<span class="comment">// class cast helper method</span>
<span class="literal">public</span> <span class="literal">static</span> &lt;T&gt; T <span class="ST6">cast</span>(Object o, Class&lt;T&gt; clazz) {
<span class="literal">return</span> clazz.isInstance(o) ? clazz.cast(o) : <span class="literal">null</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">the</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">a</span> <span class="comment">key</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">if</span> <span class="comment">it</span> <span class="comment">is</span> <span class="comment">present</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">More</span> <span class="comment">formally</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">a</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">key</span> <span class="comment">k</span> <span class="comment">to</span> <span class="comment">value</span> <span class="comment">v</span> <span class="comment">such</span> <span class="comment">that</span><span class="comment"> (</span><span class="comment">key</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> ? </span><span class="comment">k</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> : </span><span class="comment">key</span><span class="comment">.</span><span class="comment">equals</span><span class="comment">(</span><span class="comment">k</span><span class="comment">)), </span><span class="comment">that</span> <span class="comment">mapping</span> <span class="comment">is</span> <span class="comment">removed</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">The</span> <span class="comment">map</span> <span class="comment">can</span> <span class="comment">contain</span> <span class="comment">at</span> <span class="comment">most</span> <span class="comment">one</span> <span class="comment">such</span> <span class="comment">mapping</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">key</span><span class="comment"> - </span><span class="comment">key</span> <span class="comment">whose</span> <span class="comment">mapping</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">removed</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">previous</span> <span class="comment">value</span> <span class="comment">associated</span> <span class="comment">with</span> <span class="comment">key</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">there</span> <span class="comment">was</span> <span class="comment">no</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">key</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">keys</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> V <span class="ST3">remove</span>(Object key) {
String dashes = <span class="string">&quot;</span><span class="string">---------</span><span class="string">&quot;</span>;
<span class="literal">try</span> {
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> remove(</span><span class="string">&quot;</span> + key.toString() + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
} <span class="literal">catch</span> (NullPointerException ex) {
System.<span class="ST5">out</span>.println(ex);
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> remove(</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">null</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">null parameter</span><span class="string">&quot;</span>);
}
K currentKey = <span class="literal">null</span>;
V currentValue = <span class="literal">null</span>;
EntryASDV&lt;K, V&gt; currentEntry = <span class="literal">null</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; <span class="ST1">map</span>.get(i).size(); ++j) {
currentEntry = <span class="ST1">map</span>.get(i).get(j);
currentKey = currentEntry.<span class="ST1">key</span>;
currentValue = currentEntry.<span class="ST1">value</span>;
<span class="literal">if</span> (currentKey.equals(key) || currentValue.equals(key)) {
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">key: </span><span class="string">&quot;</span> + currentKey + <span class="string">&quot;</span><span class="string">; value: </span><span class="string">&quot;</span> + currentValue);
<span class="comment">// remove the entry from the map</span>
<span class="ST1">map</span>.remove(i);
<span class="comment">// remove the key from the shared key set</span>
<span class="comment">// duplicate the set so we can iterate through it and not throw a ConcurrentModificationException...</span>
Object[] iterateArray = <span class="ST1">sharedKeySet</span>.toArray();
<span class="literal">for</span> (Object o : iterateArray) {
<span class="literal">if</span> (o.equals(currentKey)) {
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">removing </span><span class="string">&quot;</span> + o);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">remove successful? </span><span class="string">&quot;</span> + <span class="ST1">sharedKeySet</span>.remove(currentKey));
<span class="literal">break</span>; <span class="comment">// we only want to delete the first one</span>
}
}
<span class="comment">// remove the value from the shared key map</span>
<span class="comment">// duplicate again...</span>
iterateArray = <span class="ST1">sharedEntrySet</span>.toArray();
<span class="literal">for</span> (Object o : iterateArray) {
<span class="literal">if</span> (o.equals(currentEntry)) {
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">removing </span><span class="string">&quot;</span> + o);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">remove successful? </span><span class="string">&quot;</span> + <span class="ST1">sharedEntrySet</span>.remove(currentEntry));
<span class="literal">break</span>; <span class="comment">// we only want to delete the first one</span>
}
}
<span class="comment">// Finally, remove the value from sharedValuesCollection</span>
<span class="comment">// duplicate again...</span>
iterateArray = <span class="ST1">sharedValuesCollection</span>.toArray();
<span class="literal">for</span> (Object o : iterateArray) {
<span class="comment">//System.out.println(o);</span>
<span class="literal">if</span> (o.equals(currentValue)) {
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">removing </span><span class="string">&quot;</span> + o);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">remove successful? </span><span class="string">&quot;</span> + <span class="ST1">sharedValuesCollection</span>.remove(o));
<span class="literal">break</span>; <span class="comment">// we only want to delete the first one</span>
}
}
<span class="comment">//for(Object o: sharedValuesCollection.toArray()) System.out.println(o); </span>
<span class="literal">return</span> currentValue;
}
}
}
<span class="literal">return</span> <span class="literal">null</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Copies</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">mappings</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">map</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">effect</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">call</span> <span class="comment">is</span> <span class="comment">equivalent</span> <span class="comment">to</span> <span class="comment">that</span> <span class="comment">of</span> <span class="comment">calling</span> <span class="comment">put</span><span class="comment">(</span><span class="comment">k</span><span class="comment">, </span><span class="comment">v</span><span class="comment">) </span><span class="comment">on</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">once</span> <span class="comment">for</span> <span class="comment">each</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">key</span> <span class="comment">k</span> <span class="comment">to</span> <span class="comment">value</span> <span class="comment">v</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">map</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">behavior</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">operation</span> <span class="comment">is</span> <span class="comment">undefined</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">modified</span> <span class="comment">while</span> <span class="comment">the</span> <span class="comment">operation</span> <span class="comment">is</span> <span class="comment">in</span> <span class="comment">progress</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">m</span><span class="comment"> - </span><span class="comment">mappings</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">stored</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">null</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">keys</span> <span class="comment">or</span> <span class="comment">values</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">null</span> <span class="comment">keys</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">putAll</span>(Map&lt;? <span class="literal">extends</span> K, ? <span class="literal">extends</span> V&gt; m) {
<span class="literal">if</span> (m == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">null parameter</span><span class="string">&quot;</span>);
}
BiConsumer consumeEachEntry = <span class="literal">new</span> BiConsumer&lt;K, V&gt;() {
MapASDV&lt;K, V&gt; <span class="ST1">mapForConsumer</span> = MapASDV.<span class="literal">this</span>;
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">accept</span>(K k, V v) {
<span class="ST1">mapForConsumer</span>.put(k, v);
}
};
m.forEach(consumeEachEntry);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">mappings</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">map</span> <span class="comment">will</span> <span class="comment">be</span> <span class="comment">empty</span> <span class="comment">after</span> <span class="comment">this</span> <span class="comment">call</span> <span class="comment">returns</span><span class="comment">.</span> <span class="comment">Any</span> <span class="comment">shared</span> <span class="comment">sets</span> <span class="comment">are</span> <span class="comment">also</span> <span class="comment">cleared</span><span class="comment">.</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">clear</span>() {
<span class="comment">// Clear everything out by redefining all internal values, and let Java&#39;s GC take care of the rest</span>
<span class="ST1">map</span> = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="ST1">sharedKeySet</span> = <span class="literal">new</span> SharedSet&lt;K&gt;();
<span class="ST1">sharedEntrySet</span> = <span class="literal">new</span> SharedSet&lt;<span class="ST2">Entry</span>&lt;K, V&gt;&gt;();
<span class="ST1">sharedValuesCollection</span> = <span class="literal">new</span> SharedCollection&lt;V&gt;();
<span class="comment">//capacity = 4;</span>
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">capacity</span>; ++i) {
<span class="ST1">map</span>.add(<span class="literal">new</span> ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;());
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">a</span> <span class="comment">Set</span> <span class="comment">view</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">keys</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">set</span> <span class="comment">is</span> <span class="comment">backed</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">so</span> <span class="comment">changes</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">are</span> <span class="comment">reflected</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">set</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">vice</span><span class="comment">-</span><span class="comment">versa</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">modified</span> <span class="comment">while</span> <span class="comment">an</span> <span class="comment">iteration</span> <span class="comment">over</span> <span class="comment">the</span> <span class="comment">set</span> <span class="comment">is</span> <span class="comment">in</span> <span class="comment">progress</span><span class="comment"> (</span><span class="comment">except</span> <span class="comment">through</span> <span class="comment">the</span> <span class="comment">iterator</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">own</span> <span class="comment">remove</span> <span class="comment">operation</span><span class="comment">), </span><span class="comment">the</span> <span class="comment">results</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">iteration</span> <span class="comment">are</span> <span class="comment">undefined</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">set</span> <span class="comment">supports</span> <span class="comment">element</span> <span class="comment">removal</span><span class="comment">, </span><span class="comment">which</span> <span class="comment">removes</span> <span class="comment">the</span> <span class="comment">corresponding</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">via</span> <span class="comment">the</span> <span class="comment">Iterator</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">Set</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">removeAll</span><span class="comment">, </span><span class="comment">retainAll</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">clear</span> <span class="comment">operations</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">support</span> <span class="comment">the</span> <span class="comment">add</span> <span class="comment">or</span> <span class="comment">addAll</span> <span class="comment">operations</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">a</span> <span class="comment">set</span> <span class="comment">view</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">keys</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> Set&lt;K&gt; <span class="ST3">keySet</span>() {
<span class="literal">return</span> <span class="literal">this</span>.<span class="ST1">sharedKeySet</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">a</span> <span class="comment">Collection</span> <span class="comment">view</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">values</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">backed</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">so</span> <span class="comment">changes</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">are</span> <span class="comment">reflected</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">collection</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">vice</span><span class="comment">-</span><span class="comment">versa</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">modified</span> <span class="comment">while</span> <span class="comment">an</span> <span class="comment">iteration</span> <span class="comment">over</span> <span class="comment">the</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">in</span> <span class="comment">progress</span><span class="comment"> (</span><span class="comment">except</span> <span class="comment">through</span> <span class="comment">the</span> <span class="comment">iterator</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">own</span> <span class="comment">remove</span> <span class="comment">operation</span><span class="comment">), </span><span class="comment">the</span> <span class="comment">results</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">iteration</span> <span class="comment">are</span> <span class="comment">undefined</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">collection</span> <span class="comment">supports</span> <span class="comment">element</span> <span class="comment">removal</span><span class="comment">, </span><span class="comment">which</span> <span class="comment">removes</span> <span class="comment">the</span> <span class="comment">corresponding</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">via</span> <span class="comment">the</span> <span class="comment">Iterator</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">Collection</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">removeAll</span><span class="comment">, </span><span class="comment">retainAll</span> <span class="comment">and</span> <span class="comment">clear</span> <span class="comment">operations</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">support</span> <span class="comment">the</span> <span class="comment">add</span> <span class="comment">or</span> <span class="comment">addAll</span> <span class="comment">operations</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> Collection&lt;V&gt; <span class="ST3">values</span>() {
<span class="literal">return</span> <span class="ST1">sharedValuesCollection</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">a</span> <span class="comment">Set</span> <span class="comment">view</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">mappings</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">set</span> <span class="comment">is</span> <span class="comment">backed</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">so</span> <span class="comment">changes</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">are</span> <span class="comment">reflected</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">set</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">vice</span><span class="comment">-</span><span class="comment">versa</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">modified</span> <span class="comment">while</span> <span class="comment">an</span> <span class="comment">iteration</span> <span class="comment">over</span> <span class="comment">the</span> <span class="comment">set</span> <span class="comment">is</span> <span class="comment">in</span> <span class="comment">progress</span><span class="comment"> (</span><span class="comment">except</span> <span class="comment">through</span> <span class="comment">the</span> <span class="comment">iterator</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">own</span> <span class="comment">remove</span> <span class="comment">operation</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">through</span> <span class="comment">the</span> <span class="comment">setValue</span> <span class="comment">operation</span> <span class="comment">on</span> <span class="comment">a</span> <span class="comment">map</span> <span class="comment">entry</span> <span class="comment">returned</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">iterator</span><span class="comment">) </span><span class="comment">the</span> <span class="comment">results</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">iteration</span> <span class="comment">are</span> <span class="comment">undefined</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">set</span> <span class="comment">supports</span> <span class="comment">element</span> <span class="comment">removal</span><span class="comment">, </span><span class="comment">which</span> <span class="comment">removes</span> <span class="comment">the</span> <span class="comment">corresponding</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">via</span> <span class="comment">the</span> <span class="comment">Iterator</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">Set</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">removeAll</span><span class="comment">, </span><span class="comment">retainAll</span> <span class="comment">and</span> <span class="comment">clear</span> <span class="comment">operations</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">support</span> <span class="comment">the</span> <span class="comment">add</span> <span class="comment">or</span> <span class="comment">addAll</span> <span class="comment">operations</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">a</span> <span class="comment">set</span> <span class="comment">view</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">mappings</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> Set&lt;<span class="ST2">Entry</span>&lt;K, V&gt;&gt; <span class="ST3">entrySet</span>() {
<span class="literal">return</span> <span class="literal">this</span>.<span class="ST1">sharedEntrySet</span>;
}
@Override
<span class="literal">public</span> String <span class="ST3">toString</span>() {
String s = <span class="string">&quot;</span><span class="string">[ </span><span class="string">&quot;</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
s += <span class="ST1">map</span>.get(i).toString() + <span class="string">&quot;</span><span class="literal">\n</span><span class="string">&quot;</span>;
}
s += <span class="string">&quot;</span><span class="string"> ]</span><span class="string">&quot;</span>;
<span class="literal">return</span> s;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Created</span> <span class="comment">a</span> <span class="comment">deep</span> <span class="comment">copy</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">MapASDV</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">deep</span> <span class="comment">copy</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> Object <span class="ST3">clone</span>() {
<span class="comment">/*</span>
<span class="comment"> MapASDV&lt;K, V&gt; clonedMap = new MapASDV&lt;K, V&gt;();</span>
<span class="comment"> //clonedMap.putAll(this);</span>
<span class="comment"> for (ListASDV&lt; EntryASDV&lt;K, V&gt;&gt; list : this.map)</span>
<span class="comment"> {</span>
<span class="comment"> ListASDV&lt; EntryASDV&lt;K, V&gt;&gt; l = (ListASDV&lt; EntryASDV&lt;K, V&gt;&gt;) list.clone();</span>
<span class="comment"> clonedMap.map.add(l); </span>
<span class="comment"> }</span>
<span class="comment"> return clonedMap;</span>
<span class="comment"> */</span>
MapASDV&lt;K, V&gt; clonedMap = <span class="literal">new</span> MapASDV&lt;K, V&gt;();
clonedMap.putAll(<span class="literal">t</span><span class="literal">his</span>);
<span class="literal">return</span> clonedMap;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST6">main</span>(String[] args) {
MapASDV&lt;String, Integer&gt; map = <span class="literal">new</span> MapASDV&lt;&gt;();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">---------------------------testing put(K, V)</span><span class="string">&quot;</span>);
map.put(<span class="string">&quot;</span><span class="string">ann</span><span class="string">&quot;</span>, <span class="number">2</span><span class="number">0</span>);
map.put(<span class="string">&quot;</span><span class="string">coco</span><span class="string">&quot;</span>, <span class="number">2</span><span class="number">5</span>);
System.<span class="ST5">out</span>.println(map);
MapASDV&lt;String, Integer&gt; clonedMap = (MapASDV&lt;String, Integer&gt;) map.clone();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing double-the-size-and-rehash by reaching 0.75 load factor with another put(K, V)</span><span class="string">&quot;</span>);
map.put(<span class="string">&quot;</span><span class="string">Jonathan</span><span class="string">&quot;</span>, <span class="number">3</span><span class="number">0</span>);
System.<span class="ST5">out</span>.println(map);
map.put(<span class="string">&quot;</span><span class="string">Jonhathan</span><span class="string">&quot;</span>, <span class="number">4</span><span class="number">5</span>);
System.<span class="ST5">out</span>.println(map);
map.put(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>, <span class="number">3</span><span class="number">3</span>);
System.<span class="ST5">out</span>.println(map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing putAll(Map&lt;K,V&gt;)</span><span class="string">&quot;</span>);
Map&lt;String, Integer&gt; anotherJavaMap = <span class="literal">new</span> HashMap();
anotherJavaMap.put(<span class="string">&quot;</span><span class="string">lion king</span><span class="string">&quot;</span>, <span class="number">4</span><span class="number">5</span>);
anotherJavaMap.put(<span class="string">&quot;</span><span class="string">HYENA</span><span class="string">&quot;</span>, <span class="number">6</span>);
map.putAll(anotherJavaMap);
System.<span class="ST5">out</span>.println(map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing containsKey</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.containsKey(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(map.containsKey(<span class="string">&quot;</span><span class="string">alexander</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing containsValue</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.containsValue(<span class="number">3</span><span class="number">3</span>));
System.<span class="ST5">out</span>.println(map.containsValue(<span class="number">3</span><span class="number">4</span>));
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing getEntryForKey</span><span class="string">&quot;</span>);
<span class="ST2">Entry</span>&lt;String, Integer&gt; e = map.getEntryForKey(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.getEntryForKey(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(map.getEntryForKey(<span class="string">&quot;</span><span class="string">Alex</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing get</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.get(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(map.get(<span class="string">&quot;</span><span class="string">Alex</span><span class="string">&quot;</span>));
<span class="literal">try</span> {
map.get(<span class="literal">n</span><span class="literal">ull</span>);
} <span class="literal">catch</span> (NullPointerException ex) {
System.<span class="ST5">out</span>.println(ex.getMessage());
}
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing getIndexForKey</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.getIndexForKey(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(map.getIndexForKey(<span class="string">&quot;</span><span class="string">Alex</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing isEmpty</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.isEmpty());
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing size</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.size());
System.<span class="ST5">out</span>.println(map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing entrySet()</span><span class="string">&quot;</span>);
Set&lt;<span class="ST2">Entry</span>&lt;String, Integer&gt;&gt; entries = map.entrySet();
System.<span class="ST5">out</span>.println(entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing keySet()</span><span class="string">&quot;</span>);
Set&lt;String&gt; keys = map.keySet();
System.<span class="ST5">out</span>.println(keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing values()</span><span class="string">&quot;</span>);
Collection&lt;Integer&gt; values = map.values();
System.<span class="ST5">out</span>.println(values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing remove( K) coco 25</span><span class="string">&quot;</span>);
map.remove(<span class="string">&quot;</span><span class="string">coco</span><span class="string">&quot;</span>);
values = map.values();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">map: </span><span class="string">&quot;</span> + map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">entries: </span><span class="string">&quot;</span> + entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">keys: </span><span class="string">&quot;</span> + keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">values: </span><span class="string">&quot;</span> + values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing Entry-Collection remove </span><span class="string">&quot;</span>);
entries.remove(e);
values = map.values();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">map: </span><span class="string">&quot;</span> + map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">entries: </span><span class="string">&quot;</span> + entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">keys: </span><span class="string">&quot;</span> + keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">values: </span><span class="string">&quot;</span> + values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing Set Keys remove </span><span class="string">&quot;</span>);
keys.remove(<span class="string">&quot;</span><span class="string">ann</span><span class="string">&quot;</span>);
values = map.values();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">map: </span><span class="string">&quot;</span> + map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">entries: </span><span class="string">&quot;</span> + entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">keys: </span><span class="string">&quot;</span> + keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">values: </span><span class="string">&quot;</span> + values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing Set Values remove </span><span class="string">&quot;</span>);
values.remove(<span class="number">4</span><span class="number">5</span>);
values = map.values();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">map: </span><span class="string">&quot;</span> + map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">entries: </span><span class="string">&quot;</span> + entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">keys: </span><span class="string">&quot;</span> + keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">values: </span><span class="string">&quot;</span> + values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing clear </span><span class="string">&quot;</span>);
map.clear();
values = map.values();
entries = map.entrySet();
keys = map.keySet();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">map: </span><span class="string">&quot;</span> + map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">entries: </span><span class="string">&quot;</span> + entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">keys: </span><span class="string">&quot;</span> + keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">values: </span><span class="string">&quot;</span> + values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing add of sets and collections </span><span class="string">&quot;</span>);
<span class="literal">try</span> {
keys.add(<span class="string">&quot;</span><span class="string">a</span><span class="string">&quot;</span>);
} <span class="literal">catch</span> (Exception ex) {
System.<span class="ST5">out</span>.println(ex.getMessage());
}
<span class="literal">try</span> {
values.add(<span class="number">3</span><span class="number">3</span>);
} <span class="literal">catch</span> (Exception ex) {
System.<span class="ST5">out</span>.println(ex.getMessage());
}
<span class="literal">try</span> {
entries.add(e);
} <span class="literal">catch</span> (Exception ex) {
System.<span class="ST5">out</span>.println(ex.getMessage());
}
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing clone</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(clonedMap);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">---------------------------testing put(K, V) AGAIN</span><span class="string">&quot;</span>);
map.put(<span class="string">&quot;</span><span class="string">Nicholas</span><span class="string">&quot;</span>, <span class="number">1</span><span class="number">00</span>);
map.put(<span class="string">&quot;</span><span class="string">a</span><span class="string">&quot;</span>, <span class="number">2</span><span class="number">00</span>);
map.put(<span class="string">&quot;</span><span class="string">b</span><span class="string">&quot;</span>, -<span class="number">20</span>);
System.<span class="ST5">out</span>.println(map);
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="MapASDV_CalebFontenot" default="default" basedir=".">
<description>Builds, tests, and runs the project MapASDV_CalebFontenot.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="MapASDV_CalebFontenot-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

Some files were not shown because too many files have changed in this diff Show More