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

io.fixprotocol.md.event.mutable.DetailTableImpl Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * Copyright 2020 FIX Protocol Ltd
 *
 * 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 io.fixprotocol.md.event.mutable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import io.fixprotocol.md.event.Context;
import io.fixprotocol.md.event.DetailProperties;
import io.fixprotocol.md.event.MarkdownUtil;
import io.fixprotocol.md.event.MutableDetailProperties;
import io.fixprotocol.md.event.MutableDetailTable;
import io.fixprotocol.md.event.MutableTableColumn;

public class DetailTableImpl implements MutableDetailTable {

  private static class DetailPropertiesImpl implements MutableDetailProperties {

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

    @Override
    public void addIntProperty(String key, int value) {
      addProperty(key, Integer.toString(value));
    }

    @Override
    public void addProperty(String key, String value) {
      if (value != null) {
        final String trimmed = MarkdownUtil.stripCell(value);
        if (!trimmed.isEmpty()) {
          properties.put(Objects.requireNonNull(key, "Missing property key").toLowerCase(),
              trimmed);
        }
      }
    }

    @Override
    public Integer getIntProperty(String key) {
      final String property = getProperty(key);
      if (property != null) {
        try {
          return Integer.valueOf(property);
        } catch (final NumberFormatException e) {
          return null;
        }
      } else
        return null;
    }

    @Override
    public Collection> getProperties() {
      return Collections.unmodifiableSet(properties.entrySet());
    }

    @Override
    public String getProperty(String key) {
      return properties.get(Objects.requireNonNull(key, "Missing property key").toLowerCase());
    }

    @Override
    public String toString() {
      return "DetailPropertiesImpl [properties=" + properties + "]";
    }
  }

  private final List propertiesList = new ArrayList<>();
  private Context parent;


  @Override
  public DetailProperties addProperties(DetailProperties detailProperties) {
    propertiesList.add(detailProperties);
    return detailProperties;
  }

  @Override
  public Context getParent() {
    return parent;
  }

  @Override
  public MutableTableColumn[] getTableColumns() {
    final Map columns = new LinkedHashMap<>();

    rows().forEach(r -> r.getProperties().forEach(p -> {
      final String key = p.getKey();
      final TableColumnImpl column = columns.get(key);
      if (column == null) {
        columns.put(key, new TableColumnImpl(key, Math.max(key.length(), p.getValue().length())));
      } else {
        column.updateLength(p.getValue().length());
      }
    }));

    final Collection values = columns.values();
    final MutableTableColumn[] array = new MutableTableColumn[values.size()];
    values.toArray(array);
    return array;
  }

  @Override
  public MutableDetailProperties newRow() {
    final DetailPropertiesImpl detailProperties = new DetailPropertiesImpl();
    addProperties(detailProperties);
    return detailProperties;
  }

  @Override
  public Collection rows() {
    return Collections.unmodifiableList(propertiesList);

  }

  @Override
  public void setParent(Context parent) {
    this.parent = parent;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy