All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.orm.compiler.TableInfoMap Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014-2015 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.way.orm.compiler;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import br.com.objectos.core.collections.ImmutableList;
import br.com.objectos.core.collections.ImmutableMap;
import br.com.objectos.way.pojo.plugin.PojoInfo;
import br.com.objectos.way.schema.info.TableInfoAnnotationInfo;
import br.com.objectos.way.testable.Equality;
import br.com.objectos.way.testable.Testable;
import br.com.objectos.way.testable.Tester;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class TableInfoMap implements Testable {

  TableInfoMap() {
  }

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

  public boolean containsPrimaryKey() {
    return false;
  }

  @Override
  public Equality isEqualTo(Object that) {
    return Tester.of(TableInfoMap.class)
        .add("values", o -> o.testableValues())
        .test(this, that);
  }

  public abstract  T onFirstEntry(TableInfoMapAction action);

  public List primaryKeyPropertyList() {
    return ImmutableList.of();
  }

  public abstract QuerySelectExpression selectExpression();

  public abstract int size();

  public abstract OrmInsertable toOrmInsertable(PojoInfo pojoInfo);

  @Override
  public String toString() {
    return testableValues().toString();
  }

  abstract List testableValues();

  public static class Builder {

    private final Map> propertyTableInfoMap = new LinkedHashMap<>();

    private Builder() {
    }

    public Builder add(OrmProperty property) {
      TableInfoAnnotationInfo tableInfo = property.tableInfo();
      propertyTableInfoMap
          .computeIfAbsent(tableInfo, (ti) -> ImmutableList.builder())
          .add(property);
      return this;
    }

    public TableInfoMap build() {
      switch (propertyTableInfoMap.size()) {
      case 1:
        Entry> entry = propertyTableInfoMap
            .entrySet()
            .iterator()
            .next();
        TableInfoAnnotationInfo tableInfo = entry.getKey();
        List propertyList = entry.getValue().build();
        return SingletonTableInfoMap.of(tableInfo, propertyList);
      default:
        ImmutableMap.Builder> map = ImmutableMap.builder();
        propertyTableInfoMap.forEach((k, v) -> map.put(k, v.build()));
        return new StandardTableInfoMap(map.build());
      }
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy