Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kulman;
- import java.util.*;
- import java.lang.*;
- public class GeometricShape implements GeometricShapeInterface {
- private List<BetterPoint> points;
- private int dimensions;
- private int counter;
- private class BetterPoint {
- private Point point;
- private int count;
- private BetterPoint(Point point, int count) {
- this.point = point;
- this.count = count;
- }
- private Point getPoint() {
- return point;
- }
- private int getCount() {
- return count;
- }
- }
- public GeometricShape() {
- this.points = new LinkedList<>();
- this.dimensions = 0;
- this.counter = 0;
- }
- /**
- * Metoda zwraca liczbę wymiarów point
- *
- * @param point sprawdzany punkt
- * @return liczba wymiarów punktu
- */
- private int checkDimensions(Point point) {
- int i = 0;
- String number = "";
- while (true) {
- try {
- point.getPosition(i);
- } catch (ArrayIndexOutOfBoundsException e) {
- for (int j = 42; j < e.toString().length(); j++) {
- number += Character.getNumericValue(e.toString().charAt(j));
- }
- return Integer.parseInt(number);
- }
- i++;
- }
- }
- /**
- * Metoda dodaje punkt. Punkt dodawany na koniec kolekcji.
- *
- * @param point dodawany punkt
- * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy nowododawany
- * punkt posiada inną liczbę wymiarów
- * niż te, które już wcześniej przed
- * nim dodano. O poprawnej liczbie
- * wymiarów decyduje <b>pierwszy</b>
- * punkt dodany do kształtu.
- */
- @Override
- public void add(Point point) throws WrongNumberOfDimensionsException {
- if(dimensions == 0) {
- points.add(new BetterPoint(point, counter));
- counter++;
- dimensions = checkDimensions(point);
- } else {
- int pointDimension = checkDimensions(point);
- if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
- points.add(new BetterPoint(point, counter));
- counter++;
- }
- }
- /**
- * Metoda usuwa punkt point, o ile taki istnieje. Jeśli w kolekcji punktów jest
- * więcej takich samych jak point, usuwany jest tylko pierwszy z nich.
- *
- * @param point punkt do usunięcia
- * @throws WrongArgumentException wyjątek zgłaszany gdy zlecane jest usunięcie
- * punktu, który nie należy do figury.
- */
- @Override
- public void remove(Point point) throws WrongArgumentException {
- for(int i = 0; i < points.size(); i++) {
- if(points.get(i).getPoint().equals(point)) {
- points.remove(i);
- return;
- }
- }
- throw new WrongArgumentException(point);
- }
- /**
- * Metoda dodaje punkt przed punktem beforePoint.
- *
- * @param point dodawany punkt
- * @param beforePoint punkt, bezpośrednio przed którym nowy należy dodać
- * @throws WrongArgumentException wyjątek zgłaszany, gdy jako
- * beforePoint przekazany został punkt,
- * który nie został wcześniej dodany do
- * figury.
- * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy liczba
- * wymiarów punktu point lub
- * beforePoint nie jest zgodna z liczbą
- * wymiarów punktów dodanych wcześniej
- * do kształtu.
- */
- @Override
- public void addBefore(Point point, Point beforePoint) throws WrongArgumentException, WrongNumberOfDimensionsException {
- int pointDimension = checkDimensions(point);
- if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
- pointDimension = checkDimensions(beforePoint);
- if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
- for(int i = 0; i < points.size(); i++) {
- if(points.get(i).getPoint().equals(beforePoint)) {
- points.add(i, new BetterPoint(point, counter));
- counter++;
- return;
- }
- }
- throw new WrongArgumentException(beforePoint);
- }
- /**
- * Metoda dodaje punkt za punktem afterPoint.
- *
- * @param point dodawany punkt
- * @param afterPoint punkt, bezpośrednio za którym nowy należy dodać
- * @throws WrongArgumentException wyjątek zgłaszany, gdy jako
- * afterPoint przekazany został punkt,
- * który nie został wcześniej dodany do
- * figury.
- * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy liczba
- * wymiarów punktu point lub afterPoint
- * nie jest zgodna z liczbą wymiarów
- * punktów dodanych wcześniej do
- * kształtu.
- */
- @Override
- public void addAfter(Point point, Point afterPoint) throws WrongNumberOfDimensionsException, WrongArgumentException {
- int pointDimension = checkDimensions(point);
- if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
- pointDimension = checkDimensions(afterPoint);
- if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
- for(int i = points.size()-1; i >= 0; i--) {
- if(points.get(i).getPoint().equals(afterPoint)) {
- if(i == points.size()-1) {
- points.add(new BetterPoint(point, counter));
- counter++;
- return;
- }
- points.add(i+1, new BetterPoint(point, counter));
- counter++;
- return;
- }
- }
- throw new WrongArgumentException(afterPoint);
- }
- /**
- * Metoda usuwa punkt przed punktem beforePoint.
- *
- * @param beforePoint punkt istniejący bezpośrednio przed beforePoint należy
- * usunąć.
- * @return Gdy punkt odniesienia istniał oraz istniał punkt do usunięcia,
- * zwracana jest referencja do usuniętego punktu.
- * @throws NoSuchPointException wyjątek zgłaszany, gdy punkt
- * beforePoint jest pierwszym z punktów
- * figury i innego punktu przed nim nie
- * ma.
- * @throws WrongArgumentException wyjątek zgłaszany, gdy punkt
- * beforePoint nie został wcześniej
- * dodany do figury.
- * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy liczba
- * wymiarów punktu beforePoint nie jest
- * zgodna z liczbą wymiarów punktów
- * dodanych wcześniej do kształtu.
- */
- @Override
- public Point removeBefore(Point beforePoint) throws NoSuchPointException, WrongNumberOfDimensionsException, WrongArgumentException {
- int pointDimension = checkDimensions(beforePoint);
- if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
- for(int i = 0; i < points.size(); i++) {
- if (points.get(i).getPoint().equals(beforePoint)) {
- if (i == 0)
- throw new NoSuchPointException(beforePoint);
- Point point = points.get(i-1).getPoint();
- points.remove(i-1);
- return point;
- }
- }
- throw new WrongArgumentException(beforePoint);
- }
- /**
- * Metoda usuwa punkt za punktem afterPoint.
- *
- * @param afterPoint punkt istniejący bezpośrednio po afterPoint należy usunąć.
- * @return Gdy punkt odniesienia istniał oraz istniał punkt do usunięcia,
- * zwracana jest referencja do usuniętego punktu.
- * @throws NoSuchPointException wyjątek zgłaszany, gdy punkt
- * afterPoint jest ostatnim z punktów
- * figury i innego punktu za nim nie
- * ma.
- * @throws WrongArgumentException wyjątek zgłaszany, gdy punkt
- * afterPoint nie został wcześniej
- * dodany do figury.
- * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy liczba
- * wymiarów punktu afterPoint nie jest
- * zgodna z liczbą wymiarów punktów
- * dodanych wcześniej do kształtu.
- */
- @Override
- public Point removeAfter(Point afterPoint) throws NoSuchPointException, WrongNumberOfDimensionsException, WrongArgumentException {
- int pointDimension = checkDimensions(afterPoint);
- if(dimensions != pointDimension) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
- for(int i = points.size()-1; i >= 0; i--) {
- if(points.get(i).getPoint().equals(afterPoint)) {
- if(i == points.size()-1) {
- throw new NoSuchPointException(afterPoint);
- }
- Point point = points.get(i+1).getPoint();
- points.remove(i+1);
- return point;
- }
- }
- throw new WrongArgumentException(afterPoint);
- }
- /**
- * Metoda zwraca aktualną listę wszystkich punktów.
- *
- * @return lista punktów
- */
- @Override
- public List<Point> get() {
- List<Point> list = new ArrayList<>(points.size());
- for(BetterPoint betterPoint : points) {
- list.add(betterPoint.getPoint());
- }
- return list;
- }
- /**
- * Metoda zwraca zbiór punktów czyli kolekcję punktów bez powtórzeń. Kolejność
- * punktów w tej kolekcji nie ma znaczenia. Powtórzenie punktu ma miejsce wtedy,
- * gdy P1.equals(P2)=true.
- *
- * @return kolekcja punktów bez powtórzeń.
- */
- @Override
- public Set<Point> getSetOfPoints() {
- return new HashSet<>(this.get());
- }
- /**
- * Metoda zwraca obiekt typu Optional zawierający (o ile istnieje) punkt,
- * którego współrzędne przekazywane są na liście positions. Jeśli istnieje wiele
- * punktów o podanych wspołrzędnych zwracany jest punkt, który został dodany
- * jako ostatni. Metoda nigdy nie może zakończyć się zwróceniem null, jeśli
- * punktu o podanych współrzędnych nie ma, należy zwrócić pusty obiekt Optional.
- *
- * @param positions lista współrzędnych
- * @return obiekt Optional zawierający (o ile istnieje) punkt, o przekazanych za
- * pomocą positions współrzędnych, w przeciwnym wypadku pusty obiekt
- * Optional.
- * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy rozmiar listy
- * jest niezgodny z liczbą wymiarów
- * punktów należących do kształtu.
- */
- @Override
- public Optional<Point> getByPosition(List<Integer> positions) throws WrongNumberOfDimensionsException {
- if(dimensions != positions.size()) throw new WrongNumberOfDimensionsException(dimensions, positions.size()+1);
- List<BetterPoint> list = new ArrayList<>();
- for(BetterPoint betterPoint : points) {
- int j = 0;
- Point point = betterPoint.getPoint();
- for(int i = 0; i < dimensions; i++) {
- if(point.getPosition(i) == positions.get(i)) {
- j++;
- }
- }
- if(j == dimensions) {
- list.add(betterPoint);
- }
- }
- if(list.size() == 0) {
- return Optional.empty();
- }
- Point point = new Point(dimensions);
- int count;
- int bestCount = 0;
- for(BetterPoint betterPoint : list) {
- count = betterPoint.getCount();
- if(count >= bestCount) {
- bestCount = count;
- point = betterPoint.getPoint();
- }
- }
- return Optional.of(point);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement