diff --git a/pom.xml b/pom.xml index f43100d..322f14a 100755 --- a/pom.xml +++ b/pom.xml @@ -58,8 +58,8 @@ maven-compiler-plugin 3.1 - 1.5 - 1.5 + 1.8 + 1.8 diff --git a/src/main/java/org/noggit/IteratorWriter.java b/src/main/java/org/noggit/IteratorWriter.java new file mode 100644 index 0000000..9f1634f --- /dev/null +++ b/src/main/java/org/noggit/IteratorWriter.java @@ -0,0 +1,64 @@ +/** + * Copyright 2006- Yonik Seeley + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.noggit; + +import java.io.IOException; + +/** + * Interface to help do push writing to an array + */ +public interface IteratorWriter { + /** + * @param iw after this method returns , the ItemWriter Object is invalid + * Do not hold a reference to this object + */ + void writeIter(ItemWriter iw) throws IOException; + + interface ItemWriter { + /** + * The item could be any supported type + */ + ItemWriter add(Object o) throws IOException; + + default ItemWriter add(int v) throws IOException { + add((Integer) v); + return this; + } + + + default ItemWriter add(long v) throws IOException { + add((Long) v); + return this; + } + + + default ItemWriter add(float v) throws IOException { + add((Float) v); + return this; + } + + default ItemWriter add(double v) throws IOException { + add((Double) v); + return this; + } + + default ItemWriter add(boolean v) throws IOException { + add((Boolean) v); + return this; + } + } + +} diff --git a/src/main/java/org/noggit/MapWriter.java b/src/main/java/org/noggit/MapWriter.java new file mode 100644 index 0000000..42ef032 --- /dev/null +++ b/src/main/java/org/noggit/MapWriter.java @@ -0,0 +1,78 @@ +package org.noggit; + +/** + * Copyright 2006- Yonik Seeley + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.IOException; + +/** + * Use this class to push all entries of a Map into an output. + * This avoids creating map instances and is supposed to be memory efficient. + * If the entries are primitives, unnecessary boxing is also avoided + */ +public interface MapWriter { + + + + void writeMap(EntryWriter ew) throws IOException; + + /** + * An interface to push one entry at a time to the output + */ + interface EntryWriter { + + /** + * Writes a key value into the map + * + * @param k The key + * @param v The value can be any supported object + */ + EntryWriter put(String k, Object v) throws IOException; + + default EntryWriter putIfNotNull(String k, Object v) throws IOException { + if(v != null) put(k,v); + return this; + } + + + default EntryWriter put(String k, int v) throws IOException { + put(k, (Integer) v); + return this; + } + + + default EntryWriter put(String k, long v) throws IOException { + put(k, (Long) v); + return this; + } + + + default EntryWriter put(String k, float v) throws IOException { + put(k, (Float) v); + return this; + } + + default EntryWriter put(String k, double v) throws IOException { + put(k, (Double) v); + return this; + } + + default EntryWriter put(String k, boolean v) throws IOException { + put(k, (Boolean) v); + return this; + } + } +}