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

jp.co.yahoo.yosegi.spread.expand.ExpandSpread Maven / Gradle / Ivy

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 jp.co.yahoo.yosegi.spread.expand; import jp.co.yahoo.yosegi.message.design.IField; import jp.co.yahoo.yosegi.message.design.StructContainerField; import jp.co.yahoo.yosegi.message.parser.IParser; import jp.co.yahoo.yosegi.spread.Spread; import jp.co.yahoo.yosegi.spread.column.ICell; import jp.co.yahoo.yosegi.spread.column.IColumn; import jp.co.yahoo.yosegi.spread.column.NullColumn; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.stream.IntStream; public class ExpandSpread extends Spread { private final Map expandColumn; private final List expandColumnList; private final Map expandColumnIndexMap; private int[] spreadIndexArray; private Spread innnerSpread; /** * Initialize. */ public ExpandSpread() { super(); expandColumn = new LinkedHashMap<>(); expandColumnList = new ArrayList<>(); expandColumnIndexMap = new HashMap<>(); } /** * Set the original Spread. */ public void setOriginalSpread( final Spread original , final int[] spreadIndexArray ) { innnerSpread = original; this.spreadIndexArray = spreadIndexArray; } /** * Add expanded columns. */ public void addExpandLeafColumn( final String linkColumnName , final IColumn arrayColumn ) { expandColumn.put( linkColumnName , arrayColumn ); expandColumnList.add( arrayColumn ); expandColumnIndexMap.put( linkColumnName , expandColumnIndexMap.size() ); } /** * Add expanded columns. */ public void addExpandColumn( final String linkColumnName , final IColumn arrayColumn , final int[] indexArray ) { IColumn column = new ExpandColumn( arrayColumn , indexArray ); expandColumn.put( linkColumnName , column ); expandColumnList.add( column ); expandColumnIndexMap.put( linkColumnName , expandColumnIndexMap.size() ); } @Override public Map getLine(final Map previous , final int index ) { Map result = innnerSpread.getLine( previous , spreadIndexArray[index] ); for ( Map.Entry entry : expandColumn.entrySet() ) { result.put( entry.getKey() , entry.getValue().get( index ) ); } return result; } @Override public int addRow( final String key , final Object row ) throws IOException { throw new UnsupportedOperationException( getReadOnlyExceptionMessage() ); } @Override public int addRow( final Map row ) { throw new UnsupportedOperationException( getReadOnlyExceptionMessage() ); } @Override public int addParserRow( final IParser parser )throws IOException { throw new UnsupportedOperationException( getReadOnlyExceptionMessage() ); } @Override public int addRows( final List> rows ) { throw new UnsupportedOperationException( getReadOnlyExceptionMessage() ); } @Override public List getListColumn() { List result = new ArrayList<>(); for ( IColumn column : innnerSpread.getListColumn() ) { result.add( new ExpandColumn( column , spreadIndexArray ) ); } result.addAll( expandColumnList ); return result; } @Override public Map getAllColumn() { Map result = new HashMap<>(); for ( Map.Entry entry : innnerSpread.getAllColumn().entrySet() ) { result.put( entry.getKey() , new ExpandColumn( entry.getValue() , spreadIndexArray ) ); } result.putAll( expandColumn ); return result; } @Override public List getColumnKeys() { List result = innnerSpread.getColumnKeys(); for ( Map.Entry entry : expandColumn.entrySet() ) { result.add( entry.getKey() ); } return result; } @Override public IColumn getColumn( final int index ) { int spreadSize = innnerSpread.getColumnSize(); if ( spreadSize <= index ) { return NullColumn.getInstance(); } else if ( index < spreadSize ) { return new ExpandColumn( innnerSpread.getColumn( index ) , spreadIndexArray ); } else if ( index < ( spreadSize + expandColumnList.size() ) ) { return expandColumnList.get( index - innnerSpread.getColumnSize() ); } else { return NullColumn.getInstance(); } } @Override public IColumn getColumn( final String columnName ) { if ( expandColumn.containsKey( columnName ) ) { return expandColumn.get( columnName ); } return new ExpandColumn( innnerSpread.getColumn( columnName ) , spreadIndexArray ); } @Override public void addColumn( final IColumn column ) { throw new UnsupportedOperationException( getReadOnlyExceptionMessage() ); } @Override public boolean containsColumn( final String columnName ) { return ( expandColumn.containsKey( columnName ) || innnerSpread.containsColumn( columnName ) ); } @Override public int getColumnIndex( final String columnName ) { if ( expandColumn.containsKey( columnName ) ) { return expandColumnIndexMap.get( columnName ) + innnerSpread.getColumnSize(); } return innnerSpread.getColumnIndex( columnName ); } @Override public void setRowCount( final int rowCount ) { throw new UnsupportedOperationException( getReadOnlyExceptionMessage() ); } @Override public int getColumnSize() { return expandColumn.size() + innnerSpread.getColumnSize(); } @Override public int size() { return spreadIndexArray.length; } @Override public IField getSchema() throws IOException { return getSchema( "root" ); } @Override public IField getSchema( final String schemaName ) throws IOException { StructContainerField schema = (StructContainerField)innnerSpread.getSchema(); for ( Map.Entry entry : expandColumn.entrySet() ) { schema.set( entry.getValue().getSchema() ); } return schema; } @Override public String toString() { StringBuffer result = new StringBuffer(); Map cache = new HashMap(); IntStream.range( 0 , size() ) .forEach( i -> { Map line = getLine( cache , i ); result.append( "--------------------------\n" ); result.append( String.format( "LINE-%d\n" , i ) ); result.append( "--------------------------\n" ); result.append( line.toString() ); result.append( "\n" ); } ); return result.toString(); } private String getReadOnlyExceptionMessage() { return "Expand spread is read only."; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy