Skip to content

Instantly share code, notes, and snippets.

@MasonGulu
Created September 11, 2022 16:57
Show Gist options
  • Save MasonGulu/63404535d8d8077d19ce1b8ad6b33b02 to your computer and use it in GitHub Desktop.
Save MasonGulu/63404535d8d8077d19ce1b8ad6b33b02 to your computer and use it in GitHub Desktop.
Very simple implementation of a Lua table in Java, not meant as a datastructure for using in Java programs, but as an export format.
/**
* SIMPLE Java implementation of Lua tables
* Mainly meant as an export format
*
* @author https://github.com/MasonGulu
*/
/**
* MIT License
*
* Copyright (c) 2022 Mason Gulu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import java.util.HashMap;
import java.util.Map;
public class LuaTable {
private final Map<Object, Object> table = new HashMap<>();
private int lastKey = 0;
private int lastKeySerialize = 0;
/**
* Put a value at a specific index
* @param key any key
* @param value any value
*/
public <KT, VT> void put(KT key, VT value) {
table.put(key, value);
}
/**
* Put a value at the next sequential integer index
* @param value any value
*/
public <VT> void put(VT value) {
table.put(++lastKey, value);
}
/**
* Get a value from a specific index
* @param key any key
* @return value
*/
public <KT> Object get(KT key) {;
return table.get(key);
}
private String getLuaRep(Object obj, boolean isKey) {
if (obj instanceof String) {
return "\""+obj+"\"";
} else if (obj instanceof Integer o) {
if (o == lastKeySerialize+1 && isKey) {
lastKeySerialize++;
return null;
}
return o.toString();
}
return obj.toString();
}
/**
* Equivalent to calling textutils.serialize on a Lua table in ComputerCraft
*
* @return String
*/
public String toString() {
// Iterate over each element in the hash map, building a string
StringBuilder str = new StringBuilder();
str.append("{");
lastKeySerialize = 0;
for (Map.Entry<Object, Object> entry : table.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
String keyStr = getLuaRep(key, true);
if (keyStr != null) {
str.append("[").append(keyStr).append("]=");
}
str.append(getLuaRep(value, false)).append(",");
}
str.append("}");
return str.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment