net.minecrell.workbench.tools.mapping.BaseMappings.java Source code

Java tutorial

Introduction

Here is the source code for net.minecrell.workbench.tools.mapping.BaseMappings.java

Source

/*
 * Copyright (c) 2015, Minecrell <https://github.com/Minecrell>
 *
 * 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.
 */
package net.minecrell.workbench.tools.mapping;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import net.minecrell.workbench.tools.mapping.name.BaseName;
import net.minecrell.workbench.tools.mapping.name.ClassName;
import net.minecrell.workbench.tools.mapping.name.FieldName;
import net.minecrell.workbench.tools.mapping.name.MethodName;
import net.minecrell.workbench.tools.mapping.name.Name;
import net.minecrell.workbench.tools.mapping.name.NameContext;

import java.util.Objects;
import java.util.function.Consumer;

public final class BaseMappings implements Mappings {

    private final NameContext context;

    private final ImmutableMap<String, BaseName> fields;
    private final ImmutableMap<String, BaseName> methods;
    private final ImmutableMap<String, String> parameters;

    public BaseMappings(NameContext context, ImmutableMap<String, BaseName> fields,
            ImmutableMap<String, BaseName> methods, ImmutableMap<String, String> parameters) {
        this.context = context;
        this.fields = fields;
        this.methods = methods;
        this.parameters = parameters;
    }

    @Override
    public NameContext getContext() {
        return context;
    }

    @Override
    public ClassName mapClass(String name) {
        return context.getClass(name);
    }

    @Override
    public ClassName map(ClassName mapping) {
        return mapping;
    }

    @Override
    public FieldName map(FieldName mapping) {
        BaseName field = fields.get(mapping.getName());
        if (field != null) {
            return mapping.apply(context, field);
        } else {
            return mapping;
        }
    }

    @Override
    public MethodName map(MethodName mapping) {
        BaseName method = methods.get(mapping.getName());
        if (method != null) {
            return mapping.apply(context, method);
        } else {
            return mapping;
        }
    }

    private void applyParameters(MethodName m) {
        if (m.hasParameters()) {
            String[] parameters = m.getParameters();
            for (int i = 0; i < parameters.length; i++) {
                String name = this.parameters.get(parameters[i]);
                if (name != null) {
                    parameters[i] = name;
                }
            }
        }
    }

    public ClassMappings apply(ClassMappings mappings) {
        return new ClassMappings(context, ImmutableBiMap.of(), apply(mappings.getFields(), this.fields, null),
                apply(mappings.getMethods(), this.methods, this::applyParameters));
    }

    @SuppressWarnings("unchecked")
    private <T extends Name> ImmutableBiMap<T, T> apply(ImmutableBiMap<T, T> mappings,
            ImmutableMap<String, BaseName> base, Consumer<T> consumer) {
        ImmutableBiMap.Builder<T, T> builder = ImmutableBiMap.builder();
        mappings.forEach((original, mapped) -> {
            BaseName name = base.get(original.getName());
            if (name != null) {
                T result = (T) original.apply(context, name);
                if (consumer != null) {
                    consumer.accept(result);
                }
                builder.put(original, result);
            }
        });
        return builder.build();
    }

    public ClassMappings applyMapped(ClassMappings mappings) {
        return new ClassMappings(context, mappings.getClasses(),
                applyMapped(mappings.getFields(), this.fields, null),
                applyMapped(mappings.getMethods(), this.methods, this::applyParameters));
    }

    @SuppressWarnings("unchecked")
    private <T extends Name> ImmutableBiMap<T, T> applyMapped(ImmutableBiMap<T, T> mappings,
            ImmutableMap<String, BaseName> base, Consumer<T> consumer) {
        ImmutableBiMap.Builder<T, T> builder = ImmutableBiMap.builder();
        mappings.forEach((original, mapped) -> {
            BaseName name = base.get(mapped.getName());
            if (name != null) {
                T result = (T) mapped.apply(context, name);
                if (consumer != null) {
                    consumer.accept(result);
                }
                builder.put(original, result);
            } else {
                builder.put(original, mapped);
            }
        });
        return builder.build();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof BaseMappings)) {
            return false;
        }

        BaseMappings that = (BaseMappings) o;
        return Objects.equals(fields, that.fields) && Objects.equals(methods, that.methods)
                && Objects.equals(parameters, that.parameters);
    }

    @Override
    public int hashCode() {
        return Objects.hash(fields, methods, parameters);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).add("fields", fields).add("methods", methods)
                .add("parameters", parameters).toString();
    }

    public static Builder builder(NameContext context) {
        return new Builder(context);
    }

    public static final class Builder {

        private final NameContext context;

        private final ImmutableMap.Builder<String, BaseName> fields = ImmutableMap.builder();
        private final ImmutableMap.Builder<String, BaseName> methods = ImmutableMap.builder();
        private final ImmutableMap.Builder<String, String> parameters = ImmutableMap.builder();

        public Builder(NameContext context) {
            this.context = context;
        }

        public NameContext getContext() {
            return context;
        }

        public ImmutableMap.Builder<String, BaseName> getFields() {
            return fields;
        }

        public ImmutableMap.Builder<String, BaseName> getMethods() {
            return methods;
        }

        public ImmutableMap.Builder<String, String> getParameters() {
            return parameters;
        }

        public void addField(String original, BaseName mapped) {
            fields.put(original, mapped);
        }

        public void addMethod(String original, BaseName mapped) {
            methods.put(original, mapped);
        }

        public void addParameter(String original, String mapped) {
            parameters.put(original, mapped);
        }

        public BaseMappings build() {
            return new BaseMappings(context, fields.build(), methods.build(), parameters.build());
        }

    }

}