diff options
| author | Ben Culkin <scorpress@gmail.com> | 2023-06-25 15:50:38 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2023-06-25 15:50:38 -0400 |
| commit | 44be6e6cd7671dd243056107ffa6201504f7fbce (patch) | |
| tree | ea6d1d0bf5930395c3b79b40b4889782dc1b5791 /src/main/java/bjc/esodata | |
| parent | 0f958b08b3446a866418aa485bb60c208d952033 (diff) | |
Update a number of things
Diffstat (limited to 'src/main/java/bjc/esodata')
| -rw-r--r-- | src/main/java/bjc/esodata/AbbrevMap2.java | 71 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/ClosedSpoolException.java | 48 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/ComplexSpooler.java | 43 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/DefaultList.java | 3 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/DefaultSpool.java | 80 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/DefaultSpooler.java | 43 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/KeyedList.java | 11 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/Multimap.java | 5 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/NestList.java | 6 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/PushdownMap.java | 4 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/Spool.java | 48 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/Spooler.java | 42 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/TSetMultimap.java | 3 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/TapeLibrary.java | 4 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/spool/Spool.java | 22 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/spool/Spooler.java | 22 |
16 files changed, 389 insertions, 66 deletions
diff --git a/src/main/java/bjc/esodata/AbbrevMap2.java b/src/main/java/bjc/esodata/AbbrevMap2.java index 72bd4c8..6605c17 100644 --- a/src/main/java/bjc/esodata/AbbrevMap2.java +++ b/src/main/java/bjc/esodata/AbbrevMap2.java @@ -18,6 +18,7 @@ package bjc.esodata; import java.util.*; +import java.util.function.Function; /** * A map that allows you to reference strings by unambiguous abbreviations to @@ -34,18 +35,65 @@ import java.util.*; * @author Ben Culkin */ public class AbbrevMap2 { + /** + * Strategy to generate abbreviations for an AbbrevMap. + * @author bjcul + * + */ + public static interface AbbrevStrategy extends Function<String, List<String>>{ + // alias iface. + } + // Stores a mapping from strings, to strings that they could be abbreviations // for private Multimap<String, String> backing; + private List<AbbrevStrategy> strategies; + + /** + * Strategy which generates abbreviations as all of the prefixes of a word + */ + public static final AbbrevStrategy INITIAL = (word) -> { + List<String> retList = new ArrayList<>(); + + int len = word.length(); + + for (int i = 1; i <= len; i++) { + String substr = word.substring(0, i); + + retList.add(substr); + } + + return retList; + }; + + /** + * Strategy which converts a camel-case word into the letters of its components. + */ + public static final AbbrevStrategy CAMELCASE = (word) -> { + List<String> retList = new ArrayList<>(); + // TODO implement a thing + return retList; + }; + /** * Create a new abbreviation map. */ public AbbrevMap2() { - backing = new TSetMultimap<>(); + this(INITIAL); } /** + * Create a new abbreviation map. + * + * @param strategies The strategies to use to generate abbreviations. + */ + public AbbrevMap2(AbbrevStrategy... strategies) { + this.backing = new TSetMultimap<>(); + this.strategies = List.of(strategies); + } + + /** * Add words to the map. * * @param words @@ -58,15 +106,11 @@ public class AbbrevMap2 { } // Generate all of the strings a given word could be abbreviated as - private static List<String> genAbbrevs(String word) { + private List<String> genAbbrevs(String word) { List<String> retList = new ArrayList<>(); - int len = word.length(); - - for (int i = 1; i <= len; i++) { - String substr = word.substring(0, i); - - retList.add(substr); + for (AbbrevStrategy strategy : strategies) { + retList.addAll(strategy.apply(word)); } return retList; @@ -88,9 +132,9 @@ public class AbbrevMap2 { * Get all of the strings that a string could be an abbreviation for. * * @param word - * The word to attempt to deabbreviate. + * The word to attempt to de-abbreviate. * - * @return All of the possible deabbreviations for that word. + * @return All of the possible de-abbreviations for that word. */ public Set<String> deabbrevAll(String word) { return backing.get(word); @@ -100,15 +144,16 @@ public class AbbrevMap2 { * Get the unambiguous thing the string is an abbreviation for. * * @param word - * The word to attempt to deabbreviate. + * The word to attempt to de-abbreviate. * - * @return The unambiguous deabbreviation of the string, or null if there isn't + * @return The unambiguous de-abbreviation of the string, or null if there isn't * one. */ public String deabbrev(String word) { Set<String> st = backing.get(word); if (st.size() == 1) return st.iterator().next(); - else return null; + + return null; } } diff --git a/src/main/java/bjc/esodata/ClosedSpoolException.java b/src/main/java/bjc/esodata/ClosedSpoolException.java new file mode 100644 index 0000000..ed94222 --- /dev/null +++ b/src/main/java/bjc/esodata/ClosedSpoolException.java @@ -0,0 +1,48 @@ +package bjc.esodata; + +/** + * Exception thrown for attempting to do something with a closed spool that you + * should not have. + * + * @author bjcul + * + */ +public class ClosedSpoolException extends RuntimeException { + private static final long serialVersionUID = 2846671972910718257L; + + /** + * Create a new default closed spool exception + */ + public ClosedSpoolException() { + super(); + } + + /** + * Create a new closed spool exception with a given message and cause. + * + * @param message The message for the exception. + * @param cause The cause of the exception. + */ + public ClosedSpoolException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Create a new closed spool exception with a given message. + * + * @param message The message for the exception + */ + public ClosedSpoolException(String message) { + super(message); + } + + /** + * Create a new closed spool exception with a given cause. + * + * @param cause The cause for the exception. + */ + public ClosedSpoolException(Throwable cause) { + super(cause); + } + +} diff --git a/src/main/java/bjc/esodata/ComplexSpooler.java b/src/main/java/bjc/esodata/ComplexSpooler.java new file mode 100644 index 0000000..095527a --- /dev/null +++ b/src/main/java/bjc/esodata/ComplexSpooler.java @@ -0,0 +1,43 @@ +package bjc.esodata; + +import java.util.*; +import java.util.function.Consumer; + +/** + * Implementation of {@link Spooler} which supports a number of additional features. + * + * @author bjcul + * + * @param <T> The type of data contained on the spools + */ +public class ComplexSpooler<T> implements Spooler<T> { + private Queue<Spool<T>> pendingAnonSpools; + private Map<String, Queue<Spool<T>>> pendingGroupedSpools; + + private Map<String, Spool<T>> labelledSpools; + + @Override + public void registerSpool(Spool<T> spool) { + // TODO Auto-generated method stub + + } + + @Override + public Consumer<T> getInput() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Iterator<T> getOutput() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Spool<T> getSpool() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/bjc/esodata/DefaultList.java b/src/main/java/bjc/esodata/DefaultList.java index 727eafd..cde6ed4 100644 --- a/src/main/java/bjc/esodata/DefaultList.java +++ b/src/main/java/bjc/esodata/DefaultList.java @@ -108,7 +108,8 @@ public class DefaultList<ValueType> extends AbstractList<ValueType> { @Override public ValueType get(int idx) { if (idx < 0 || idx >= backing.size()) return defVal; - else return backing.get(idx); + + return backing.get(idx); } @Override diff --git a/src/main/java/bjc/esodata/DefaultSpool.java b/src/main/java/bjc/esodata/DefaultSpool.java new file mode 100644 index 0000000..372275d --- /dev/null +++ b/src/main/java/bjc/esodata/DefaultSpool.java @@ -0,0 +1,80 @@ +package bjc.esodata; + +import java.util.Iterator; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.function.Consumer; + +/** + * A default implementation of {@link Spool} using a {@link LinkedBlockingDeque} + * as the backing storage. + * + * @author bjcul + * + * @param <T> The type contained in the spool + */ +public class DefaultSpool<T> implements Spool<T>, AutoCloseable { + private String label; + private String group; + + private BlockingQueue<T> container; + + private boolean closed; + + /** + * Create a new spool without label or group + */ + public DefaultSpool() { + this(null, null); + } + + /** + * Create a new spool with a given label and group. + * + * @param label The label of the spool + * @param group The group for the spool + */ + public DefaultSpool(String label, String group) { + this.label = label; + this.group = group; + + this.container = new LinkedBlockingDeque<>(); + this.closed = false; + } + + @Override + public String label() { + return label; + } + + @Override + public String group() { + return group; + } + + @Override + public Consumer<T> getInput() { + return (val) -> { + if (closed) { + throw new ClosedSpoolException(); + } + container.add(val); + }; + } + + @Override + public Iterator<T> getOutput() { + return container.iterator(); + } + + @Override + public boolean isClosed() { + return false; + } + + @Override + public void close() throws Exception { + closed = true; + } + +} diff --git a/src/main/java/bjc/esodata/DefaultSpooler.java b/src/main/java/bjc/esodata/DefaultSpooler.java new file mode 100644 index 0000000..8ac7891 --- /dev/null +++ b/src/main/java/bjc/esodata/DefaultSpooler.java @@ -0,0 +1,43 @@ +package bjc.esodata; + +import java.util.*; +import java.util.function.Consumer; + +/** + * A default implementation of {@link Spooler} that handles spools in a FIFO manner + * @author bjcul + * + * @param <T> The type contained in the spools. + */ +public class DefaultSpooler<T> implements Spooler<T> { + private Queue<Spool<T>> spools; + + /** + * Create a new default spooler. + */ + public DefaultSpooler() { + this.spools = new ArrayDeque<>(); + } + + @Override + public void registerSpool(Spool<T> spool) { + spools.add(spool); + } + + @Override + public Consumer<T> getInput() { + Spool<T> spool = new DefaultSpool<>(); + registerSpool(spool); + return spool.getInput(); + } + + @Override + public Iterator<T> getOutput() { + return spools.remove().getOutput(); + } + + @Override + public Spool<T> getSpool() { + return spools.remove(); + } +} diff --git a/src/main/java/bjc/esodata/KeyedList.java b/src/main/java/bjc/esodata/KeyedList.java index 5b0d6cc..5a381e1 100644 --- a/src/main/java/bjc/esodata/KeyedList.java +++ b/src/main/java/bjc/esodata/KeyedList.java @@ -19,12 +19,23 @@ package bjc.esodata; import java.util.*; +/** + * Implements a keyed list. + * + * @author bjcul + * + * @param <Key> The type of the key + * @param <Val> The type of the value + */ public class KeyedList<Key, Val> implements Iterable<Val> { private List<Val> backing; private Map<Key, Integer> indices; private int currIdx = 0; + /** + * Create a new keyed list + */ public KeyedList() { backing = new ArrayList<>(); indices = new HashMap<>(); diff --git a/src/main/java/bjc/esodata/Multimap.java b/src/main/java/bjc/esodata/Multimap.java index d0c0b4b..e9c19bb 100644 --- a/src/main/java/bjc/esodata/Multimap.java +++ b/src/main/java/bjc/esodata/Multimap.java @@ -97,6 +97,11 @@ public interface Multimap<KeyType, ValueType> { */ boolean contains(KeyType key, ValueType value); + /** + * Retrieve an iterator over the values in this map + * + * @return An iterator over the values in this map + */ Iterator<Pair<KeyType, ValueType>> iterator(); }
\ No newline at end of file diff --git a/src/main/java/bjc/esodata/NestList.java b/src/main/java/bjc/esodata/NestList.java index c1544ca..d45229f 100644 --- a/src/main/java/bjc/esodata/NestList.java +++ b/src/main/java/bjc/esodata/NestList.java @@ -345,10 +345,10 @@ public class NestList<Element> extends AbstractList<Either<Element, NestList<Ele subst.addItem(sublist); } })); - } else { - state.addItem(list); - return state; } + + state.addItem(list); + return state; }); } // List methods and other things. diff --git a/src/main/java/bjc/esodata/PushdownMap.java b/src/main/java/bjc/esodata/PushdownMap.java index a6e153c..e9ff4de 100644 --- a/src/main/java/bjc/esodata/PushdownMap.java +++ b/src/main/java/bjc/esodata/PushdownMap.java @@ -163,9 +163,9 @@ public class PushdownMap<KeyType, ValueType> implements MapEx<KeyType, ValueType if (thawEnabled) { isFrozen = false; return true; - } else { - return false; } + + return false; } @Override diff --git a/src/main/java/bjc/esodata/Spool.java b/src/main/java/bjc/esodata/Spool.java new file mode 100644 index 0000000..f065093 --- /dev/null +++ b/src/main/java/bjc/esodata/Spool.java @@ -0,0 +1,48 @@ +package bjc.esodata; + +import java.util.Iterator; +import java.util.function.Consumer; + +/** + * A single spool, or batch of input. + * + * @author bjcul + * + * @param <T> The type contained by the spool + */ +public interface Spool<T> { + /** + * Get the label that identifies this spool, if it has one. + * + * @return The label of the spool, or null if it is unlabeled. + */ + public String label(); + + /** + * Get the group that this spool belongs to, if it belongs to one. + * + * @return The group the spool is in, or null if it is not in a group. + */ + public String group(); + + /** + * Get a function that will add input to this spool + * + * @return A function that will add input to this spool. + */ + public Consumer<T> getInput(); + + /** + * Get an iterator that will iterate over the data contained in this spool. + * + * @return An iterator over the data in this spool. + */ + public Iterator<T> getOutput(); + + /** + * Is this spool closed, meaning it can accept no more input? + * + * @return Whether the spool is closed + */ + public boolean isClosed(); +}
\ No newline at end of file diff --git a/src/main/java/bjc/esodata/Spooler.java b/src/main/java/bjc/esodata/Spooler.java new file mode 100644 index 0000000..2cf415a --- /dev/null +++ b/src/main/java/bjc/esodata/Spooler.java @@ -0,0 +1,42 @@ +package bjc.esodata; + +import java.util.Iterator; +import java.util.function.Consumer; + +/** + * Represents a 'spool' facility for gathering batches of input together to be + * processed. + * + * @author bjcul + * + * @param <T> The type of data held in the spools + */ +public interface Spooler<T> { + /** + * Register a pre-existing spool with this spooler. + * + * @param spool The spool to register + */ + public void registerSpool(Spool<T> spool); + + /** + * Get a consumer belonging to a new anonymous spool. + * + * @return A new anonymous spool. + */ + public Consumer<T> getInput(); + + /** + * Get the output for a unprocessed spool. + * + * @return The output for a unprocessed spool. + */ + public Iterator<T> getOutput(); + + /** + * Get an unprocessed spool. + * + * @return An unprocessed spool + */ + public Spool<T> getSpool(); +} diff --git a/src/main/java/bjc/esodata/TSetMultimap.java b/src/main/java/bjc/esodata/TSetMultimap.java index 1a04a41..6c925ab 100644 --- a/src/main/java/bjc/esodata/TSetMultimap.java +++ b/src/main/java/bjc/esodata/TSetMultimap.java @@ -94,7 +94,8 @@ public class TSetMultimap<KeyType, ValueType> implements Iterable<Pair<KeyType, @Override public Set<ValueType> get(KeyType key) { if (!backing.containsKey(key)) return new HashSet<>(); - else return backing.get(key).values(); + + return backing.get(key).values(); } @Override diff --git a/src/main/java/bjc/esodata/TapeLibrary.java b/src/main/java/bjc/esodata/TapeLibrary.java index 5fec28a..3794dd5 100644 --- a/src/main/java/bjc/esodata/TapeLibrary.java +++ b/src/main/java/bjc/esodata/TapeLibrary.java @@ -132,9 +132,9 @@ public class TapeLibrary<ElementType> implements TapeView<ElementType> (ignored) -> new SingleTape<>()); return true; - } else { - return false; } + + return false; } /** diff --git a/src/main/java/bjc/esodata/spool/Spool.java b/src/main/java/bjc/esodata/spool/Spool.java deleted file mode 100644 index 80e8a8c..0000000 --- a/src/main/java/bjc/esodata/spool/Spool.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * esodata - data structures and other things, of varying utility - * Copyright 2022, Ben Culkin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ -package bjc.esodata.spool; - -public interface Spool<Contained> { - -} diff --git a/src/main/java/bjc/esodata/spool/Spooler.java b/src/main/java/bjc/esodata/spool/Spooler.java deleted file mode 100644 index 133df2e..0000000 --- a/src/main/java/bjc/esodata/spool/Spooler.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * esodata - data structures and other things, of varying utility - * Copyright 2022, Ben Culkin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ -package bjc.esodata.spool; - -public interface Spooler { - public <E> Spool<E> getSpool(); -} |
