Search in sources :

Example 1 with MapObject_

use of suite.object.MapObject_ in project suite by stupidsing.

the class Dump method toLine.

private static void toLine(Object node, Sink<String> sink) {
    var dumpedObjects = new IdentityHashMap<Object, Object>();
    new Object() {

        private void d(Object object, String suffix) {
            if (object == null)
                sink.f("null");
            else if (dumpedObjects.put(object, true) == null)
                try {
                    d_(object);
                } finally {
                    dumpedObjects.remove(object);
                }
            else
                sink.f("(recursed)");
            sink.f(suffix);
        }

        private void d_(Object object) {
            var clazz = object.getClass();
            if (clazz.isArray()) {
                sink.f("[");
                for (var i = 0; i < Array.getLength(object); i++) d(Array.get(object, i), ",");
                sink.f("]");
            } else if (Util.isSimple(clazz))
                sink.f(object.toString());
            else
                new // 
                Switch<Object>(// 
                object).doIf(Collection.class, collection -> {
                    sink.f("[");
                    for (var object1 : collection) d(object1, ",");
                    sink.f("]");
                }).doIf(Map.class, map -> {
                    sink.f("{");
                    for (var e : ((Map<?, ?>) object).entrySet()) {
                        d(e.getKey(), ":");
                        d(e.getValue(), ",");
                    }
                    sink.f("}");
                }).doIf(MapObject.class, mi -> {
                    sink.f(mi.getClass().getSimpleName());
                    sink.f("{");
                    for (var object1 : MapObject_.list(mi)) d(object1, ",");
                    sink.f("}");
                }).doIf(Pair.class, pair -> {
                    sink.f("<");
                    d(pair.k, "|");
                    d(pair.v, ">");
                }).doIf(Object.class, o -> {
                    sink.f(o.getClass().getSimpleName());
                    sink.f("{");
                    for (var pair : readers(object)) {
                        Object value;
                        try {
                            value = pair.v.call();
                        } catch (Throwable ex) {
                            value = "<" + ex.getClass() + ">";
                        }
                        if (value != null) {
                            sink.f(pair.k + ":");
                            d(value, ",");
                        }
                    }
                    sink.f("}");
                }).nonNullResult();
        }
    }.d(node, "");
}
Also used : Pair(primal.adt.Pair) Array(java.lang.reflect.Array) IdentityHashMap(java.util.IdentityHashMap) Singleton(suite.node.util.Singleton) Build(primal.Verbs.Build) Collection(java.util.Collection) Util(suite.util.Util) Callable(java.util.concurrent.Callable) MapObject_(suite.object.MapObject_) Log_(primal.os.Log_) Assoc(primal.parser.Operator.Assoc) Sink(primal.fp.Funs.Sink) Read(primal.MoreVerbs.Read) Tree(suite.node.Tree) Streamlet(primal.streamlet.Streamlet) SmartSplit(suite.util.SmartSplit) List(java.util.List) MapObject(suite.object.MapObject) Map(java.util.Map) Substring(primal.Verbs.Substring) Get(primal.Verbs.Get) Switch(suite.util.Switch) Left(primal.Verbs.Left) IdentityHashMap(java.util.IdentityHashMap) MapObject(suite.object.MapObject) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map) Pair(primal.adt.Pair)

Aggregations

Array (java.lang.reflect.Array)1 Collection (java.util.Collection)1 IdentityHashMap (java.util.IdentityHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 Read (primal.MoreVerbs.Read)1 Build (primal.Verbs.Build)1 Get (primal.Verbs.Get)1 Left (primal.Verbs.Left)1 Substring (primal.Verbs.Substring)1 Pair (primal.adt.Pair)1 Sink (primal.fp.Funs.Sink)1 Log_ (primal.os.Log_)1 Assoc (primal.parser.Operator.Assoc)1 Streamlet (primal.streamlet.Streamlet)1 Tree (suite.node.Tree)1 Singleton (suite.node.util.Singleton)1 MapObject (suite.object.MapObject)1 MapObject_ (suite.object.MapObject_)1