Last active
August 29, 2015 13:56
-
-
Save henvic/8968962 to your computer and use it in GitHub Desktop.
ActorRepoArray.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Repos.Array; | |
import Interfaces.ActorRepoInterface; | |
import Entities.Actor; | |
public class ActorRepoArray implements ActorRepoInterface { | |
private Actor[] actors; | |
@Override | |
public boolean add(Actor actor) { | |
int length = actors.length; | |
Actor[] newActors; | |
for (int counter = 0; counter < length; counter += 1) { | |
if (actors[counter] == null) { | |
actors[counter] = actor; | |
return true; | |
} | |
} | |
newActors = new Actor[length + 1]; | |
System.arraycopy(actors, 0, newActors, 0, length); | |
this.actors = newActors; | |
actors[length] = actor; | |
return true; | |
} | |
@Override | |
public boolean remove(String id) { | |
for (int counter = 0, length = actors.length; counter < length; counter += 1) { | |
if (actors[counter] != null && actors[counter].getId().equalsIgnoreCase(id)) { | |
actors[counter] = null; | |
return true; | |
} | |
} | |
return false; | |
} | |
@Override | |
public Actor get(String id) { | |
for (Actor current : actors) { | |
if (current != null && current.getId().equalsIgnoreCase(id)) { | |
return current; | |
} | |
} | |
return null; | |
} | |
@Override | |
public boolean has(String id) { | |
return (this.get(id) != null); | |
} | |
public boolean replace(String id, Actor actor) { | |
for (int counter = 0, length = actors.length; counter < length; counter += 1) { | |
if (actors[counter] != null && actors[counter].getId().equalsIgnoreCase(id)) { | |
actors[counter] = actor; | |
return true; | |
} | |
} | |
return false; | |
} | |
public ActorRepoArray() { | |
this.actors = new Actor[1]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Repos.List; | |
import Entities.Actor; | |
import Interfaces.ActorRepoInterface; | |
public class ActorRepoList implements ActorRepoInterface { | |
private Actor current = null; | |
private ActorRepoList next = null; | |
public ActorRepoList() { | |
} | |
public ActorRepoList(Actor item) { | |
this.current = item; | |
} | |
public boolean add(Actor item) { | |
if (this.next == null) { | |
this.next = new ActorRepoList(item); | |
return true; | |
} | |
this.next.add(item); | |
return true; | |
} | |
public boolean remove(String id) { | |
if (this.current != null && this.current.getId().equalsIgnoreCase(id)) { | |
if (this.next != null) { | |
this.current = this.next.current; | |
this.next = this.next.next; | |
return true; | |
} | |
this.current = null; | |
return true; | |
} | |
if (this.next != null) { | |
this.next.remove(id); | |
return true; | |
} | |
return false; | |
} | |
public Actor get(String id) { | |
if (this.current != null && this.current.getId().equalsIgnoreCase(id)) { | |
return this.current; | |
} | |
if (this.next != null) { | |
return this.next.get(id); | |
} | |
return null; | |
} | |
public boolean has(String id) { | |
return this.get(id) != null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment