Skip to content

Commit

Permalink
Lists starter S2019
Browse files Browse the repository at this point in the history
  • Loading branch information
jjfiv committed Mar 4, 2019
0 parents commit 4679efa
Show file tree
Hide file tree
Showing 19 changed files with 2,837 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.idea
*.iml
*.jar
*.class
/bin
/target
.classpath
.project
.settings
*.gif
108 changes: 108 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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.smith.cs.csc212</groupId>
<artifactId>Lists</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javaVersion>1.8</javaVersion>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<type>jar</type>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>

<build>
<plugins>

<!-- build a jar with relative classpath -->
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<classpathPrefix>lib/</classpathPrefix>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>generate-javadoc</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- this is entirely to set the java version -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${javaVersion}</source>
<target>${javaVersion}</target>
</configuration>
</plugin>

<!-- copy dependencies to relative classpath -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

<plugin> <!-- Create sources jar -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

</project>
190 changes: 190 additions & 0 deletions src/main/java/edu/smith/cs/csc212/adtr/ListADT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package edu.smith.cs.csc212.adtr;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import edu.smith.cs.csc212.adtr.errors.BadIndexError;
import edu.smith.cs.csc212.adtr.errors.EmptyListError;

/**
* This is an abstract view of a List data structure.
*
* @author jfoley
*
* @param <ItemType> - the type of the item stored in this list.
*/
public abstract class ListADT<ItemType> implements Iterable<ItemType> {

/**
* Is this list of size zero? Might be easier than counting all the elements.
* @return true if this list is empty.
*/
protected abstract boolean isEmpty();

/**
* The size of this list.
*
* @return the size of the list or number of items.
*/
public abstract int size();

/**
* Set the item stored at the given index to value.
*
* @param index - the index; from 0 to size() inclusive.
* @param value - the value to put in the list.
*/
public abstract void setIndex(int index, ItemType value);

/**
* Get the value at the given index.
*
* @param index - the item index to retrieve.
* @return the value stored at that position.
*/
public abstract ItemType getIndex(int index);

/**
* Get the first item in the list.
*
* @return this.getIndex(0);
*/
public abstract ItemType getFront();

/**
* Get the last item in the list.
*
* @return this.getIndex(this.size()-1);
*/
public abstract ItemType getBack();

/**
* Add an item with value before the item at the given index.
*
* @param index - a number from 0 to size (inclusive).
* @param value - the value to insert into the list.
*/
public abstract void addIndex(int index, ItemType value);

/**
* Add value to the front of the list.
*
* @param value - the item to add.
*/
public abstract void addFront(ItemType value);

/**
* Add value to the back of hte list.
*
* @param value - the item to add.
*/
public abstract void addBack(ItemType value);

/**
* Remove the item from the list at the given index.
*
* @param index - a number from 0 to size (exclusive).
* @return the value removed.
*/
public abstract ItemType removeIndex(int index);

/**
* Remove the item from the back of the list.
*
* @return the value removed.
*/
public abstract ItemType removeBack();

/**
* Remove the item from the front of the list.
*
* @return the value removed.
*/
public abstract ItemType removeFront();

/**
* Java requires this method for it's "for (ItemType x : list) { }" loop.
*/
public Iterator<ItemType> iterator() {
return new ListADTIterator<>(this);
}

/**
* Convert this to a Java data structure; probably useful for unit-test errors.
* @return - a Java List object.
*/
public List<ItemType> toJava() {
List<ItemType> output = new ArrayList<>();
for (ItemType x : this) {
output.add(x);
}
return output;
}

/**
* If this list is empty, throw an error; useful for implementing classes.
*/
protected void checkNotEmpty() {
if (this.isEmpty()) {
throw new EmptyListError();
}
}

/**
* Check the index for a get/set/remove method.
* Not for LinkedList classes!
*/
protected void checkExclusiveIndex(int index) {
if (index < 0) {
throw new BadIndexError(index);
}
if (index >= size()) {
throw new BadIndexError(index);
}
}

/**
* Check the index for an add method.
* Not for LinkedList classes!
*/
protected void checkInclusiveIndex(int index) {
if (index < 0) {
throw new BadIndexError(index);
}
if (index > size()) {
throw new BadIndexError(index);
}
}

@Override
public String toString() {
StringBuilder out = new StringBuilder();
out.append('[');
boolean first = true;
for (ItemType x : this) {
if (first) {
first = false;
} else {
out.append(", ");
}
out.append(x);
}
out.append(']');
return out.toString();
}

@SuppressWarnings("rawtypes")
@Override
public boolean equals(Object other) {
if (other instanceof List) {
return Objects.equals(this.toJava(), other);
} else if (other instanceof ListADT) {
return Objects.equals(this.toJava(), ((ListADT) other).toJava());
} else {
return false;
}
}

}
46 changes: 46 additions & 0 deletions src/main/java/edu/smith/cs/csc212/adtr/ListADTIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package edu.smith.cs.csc212.adtr;

import java.util.Iterator;

/**
* This is an object that walks/loops/iterates/traverses through a ListADT type.
*
* @author jfoley
*
* @param <ItemType>
*/
public class ListADTIterator<ItemType> implements Iterator<ItemType> {
/**
* Which list are we walking through?
*/
ListADT<ItemType> source;
/**
* How far are we?
*/
int i = 0;

/**
* Construct this kind of object from a ListADT object.
*
* @param list - the list to loop/traverse/iterate over.
*/
public ListADTIterator(ListADT<ItemType> list) {
this.source = list;
}

/**
* Does this iterator have more data?
*/
@Override
public boolean hasNext() {
return i < source.size();
}

/**
* Get me the next item to use in my loop.
*/
@Override
public ItemType next() {
return source.getIndex(i++);
}
}
14 changes: 14 additions & 0 deletions src/main/java/edu/smith/cs/csc212/adtr/errors/BadIndexError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package edu.smith.cs.csc212.adtr.errors;
/**
* This class defines our own special error for when an operation is called on
* an list that doesn't have that index.
*
* @author jfoley
*
*/
@SuppressWarnings("serial")
public class BadIndexError extends RuntimeException {
public BadIndexError(int index) {
super("BadIndexError at "+index);
}
}
15 changes: 15 additions & 0 deletions src/main/java/edu/smith/cs/csc212/adtr/errors/EmptyListError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package edu.smith.cs.csc212.adtr.errors;

/**
* This class defines our own special error for when an operation is called on
* an empty list that doesn't make sense.
*
* @author jfoley
*
*/
@SuppressWarnings("serial")
public class EmptyListError extends RuntimeException {
public EmptyListError() {
super("EmptyListError");
}
}
Loading

0 comments on commit 4679efa

Please sign in to comment.