|
| 1 | +package com.temporaryteam.noticeditor.model; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import javafx.collections.FXCollections; |
| 5 | +import javafx.collections.ObservableList; |
| 6 | + |
| 7 | +/** |
| 8 | + * Collection of notice statuses |
| 9 | + * @author Maximillian M. |
| 10 | + */ |
| 11 | +public class NoticeStatusList { |
| 12 | + private static ArrayList<NoticeStatus> list; |
| 13 | + private static int lastIndex; |
| 14 | + |
| 15 | + private static ArrayList<NoticeStatus> mementoList; |
| 16 | + private static int mementoLastIndex; |
| 17 | + |
| 18 | + static { |
| 19 | + list = new ArrayList<>(); |
| 20 | + lastIndex = -1; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Saves status list |
| 25 | + */ |
| 26 | + public static void save() { |
| 27 | + mementoList = new ArrayList<>(list); |
| 28 | + mementoLastIndex = lastIndex; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Restores statuses to saved state |
| 33 | + */ |
| 34 | + public static void restore() { |
| 35 | + list = mementoList; |
| 36 | + lastIndex = mementoLastIndex; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Adds new status with next status code |
| 41 | + * @param statusName Status name |
| 42 | + */ |
| 43 | + public static void add(String statusName) { |
| 44 | + ++lastIndex; |
| 45 | + NoticeStatus newStatus = new NoticeStatus(statusName, lastIndex); |
| 46 | + list.add(newStatus); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Adds new status with specified status code |
| 51 | + * @param statusName Status name |
| 52 | + * @param statusCode Status code |
| 53 | + */ |
| 54 | + public static void add(String statusName, int statusCode) { |
| 55 | + lastIndex = (lastIndex < statusCode) ? statusCode : lastIndex + 1; |
| 56 | + NoticeStatus newStatus = new NoticeStatus(statusName, statusCode); |
| 57 | + list.add(newStatus); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Clears status list |
| 62 | + */ |
| 63 | + public static void clear() { |
| 64 | + list = new ArrayList<>(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns status by code |
| 69 | + * @param statusCode Status code |
| 70 | + * @return Status |
| 71 | + */ |
| 72 | + public static NoticeStatus getStatus(int statusCode) { |
| 73 | + NoticeStatus foo = list.stream().filter(status -> status.getCode() == statusCode).findFirst().orElse(list.get(0)); |
| 74 | + return foo; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns status code by name |
| 79 | + * @param statusName Status name |
| 80 | + * @return Status code |
| 81 | + */ |
| 82 | + public static int getStatusCode(String statusName) { |
| 83 | + for (NoticeStatus status : list) { |
| 84 | + if (status.getName().equals(statusName)) { |
| 85 | + return status.getCode(); |
| 86 | + } |
| 87 | + } |
| 88 | + return 0; |
| 89 | + } |
| 90 | + |
| 91 | + public static ObservableList<NoticeStatus> asObservable() { |
| 92 | + return FXCollections.observableList(list); |
| 93 | + } |
| 94 | +} |
0 commit comments