Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.skypro.ArrayList.exception;

public class ElementNotFoundException extends RuntimeException{
public ElementNotFoundException() {}

public ElementNotFoundException(String message) {
super(message);
}

public ElementNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public ElementNotFoundException(Throwable cause) {
super(cause);
}

public ElementNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.skypro.ArrayList.exception;

public class InvalidIndexException extends RuntimeException{
public InvalidIndexException() {}

public InvalidIndexException(String message) {
super(message);
}

public InvalidIndexException(String message, Throwable cause) {
super(message, cause);
}

public InvalidIndexException(Throwable cause) {
super(cause);
}

public InvalidIndexException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

}
21 changes: 21 additions & 0 deletions src/main/java/ru/skypro/ArrayList/exception/NullItemException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.skypro.ArrayList.exception;

public class NullItemException extends RuntimeException {
public NullItemException() {
}
public NullItemException(String message) {
super (message);
}

public NullItemException(String message, Throwable cause) {
super(message, cause);
}

public NullItemException(Throwable cause) {
super(cause);
}

public NullItemException(String message, Throwable cause, boolean enableSuppression, boolean writableStrackTrace) {
super (message, cause, enableSuppression, writableStrackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.skypro.ArrayList.exception;

public class StorageIsFullException extends RuntimeException {
public StorageIsFullException() {
}

public StorageIsFullException(String message) {
super(message);
}

public StorageIsFullException(String message, Throwable cause) {
super(message, cause);
}

public StorageIsFullException(Throwable cause) {
super(cause);
}

public StorageIsFullException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

}
23 changes: 23 additions & 0 deletions src/main/java/ru/skypro/ArrayList/service/StringList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.skypro.ArrayList.service;

import java.util.Collection;

public interface StringList {
String add(String item);
String add(int index, String item);
String set(int index, String item);
String remove(String item);
String remove(int index);
boolean contains(String item);
int indexOf(String item);
int lastIndexOf(String item);
String get(int index);
boolean equals(StringList otherList);
int size();
boolean isEmpty();
void clear();

void clear(Collection c);

String[] toArray();
}
166 changes: 166 additions & 0 deletions src/main/java/ru/skypro/ArrayList/service/impl/StringListImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package ru.skypro.ArrayList.service.impl;

import ru.skypro.ArrayList.exception.ElementNotFoundException;
import ru.skypro.ArrayList.exception.InvalidIndexException;
import ru.skypro.ArrayList.exception.NullItemException;
import ru.skypro.ArrayList.exception.StorageIsFullException;
import ru.skypro.ArrayList.service.StringList;

import java.util.Arrays;
import java.util.Collection;

public class StringListImpl implements StringList {
private final String[] storage;
private int index ;
private String item;
public int size;

public StringListImpl() {
storage = new String[10];
}

public StringListImpl(int initSize) {
storage = new String[initSize];
}

@Override
public String add(String item) {
validateItem(item);
validateSize();
storage[size++] = item;
return item;
}

@Override
public String add(int index, String item) {
validateSize();
validateItem(item);
validateIndex(index);
if (index == size) {
storage [size++] = item;
return item;
}
System.arraycopy(storage, index, storage, index +1, size - index);
storage[index] = item;
size++;
return item;
}

@Override
public String set(int index, String item) {
validateIndex (index);
validateItem (item);
storage[index] = item;
return item;
}

@Override
public String remove(String item) {
validateItem(item);

int index = indexOf(item);
if (index == -1) {
throw new ElementNotFoundException();
}

if (index != size) {
System.arraycopy(storage, index + 1, storage, index, size - index);
}

size--;
return item;
}

@Override
public String remove(int index) {
validateIndex(index);

String item = storage[index];

if (index != size) {
System.arraycopy(storage, index + 1, storage, index, size - index);
}

size--;
return item;
}

@Override
public boolean contains(String item) {
return indexOf(item) != -1;
}

@Override
public int indexOf(String item) {
for (int i = 0; i < size; i++) {
if (storage[i].equals(item)) {
return i;
}
}
return -1;
}

@Override
public int lastIndexOf(String item) {
for (int i = size - 1; i > 0; i--) {
if (storage[i].equals(item)) {
return i;
}
}
return -1;
}

@Override
public String get(int index) {
validateIndex (index);
return storage[index];
}

@Override
public boolean equals(StringList otherList) {
return Arrays.equals (this.toArray(), otherList.toArray());
}

@Override
public int size() {
return size;
}

@Override
public boolean isEmpty() {
return size == 0;
}

@Override
public void clear() {
size = 0;
}

@Override
public void clear(Collection c) {
size = 0;
}

@Override
public String[] toArray() {
return Arrays.copyOf(storage, size);
}

private void validateItem(String item) {
if (item == null) {
throw new NullItemException();
}
}

private void validateSize() {
if (size == storage.length) {
throw new StorageIsFullException();
}
}

private void validateIndex (int index) {
if (index < 0 || index > size) {
throw new InvalidIndexException();
}
}
}