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

com.inamik.text.tables.cell.base.FunctionWithWidth Maven / Gradle / Ivy

The newest version!
/*
 * iNamik Text Tables for Java
 * 
 * Copyright (C) 2016 David Farrell ([email protected])
 *
 * Licensed under The MIT License (MIT), see LICENSE.txt
 */
package com.inamik.text.tables.cell.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public abstract class FunctionWithWidth
{
    public abstract Collection apply(Integer width, Collection cell);

    public static final FunctionWithWidth IDENTITY = new FunctionWithWidth() {
        @Override
        public Function withWidth(int _1) {
            return Function.IDENTITY;
        }
        @Override
        public Collection apply(Integer height, Collection cell) {
            return cell;
        }
    };

    /*
     * From line.FunctionWithWidth
     */
    public static FunctionWithWidth from(final com.inamik.text.tables.line.base.FunctionWithWidth f) {
        return new FunctionWithWidth() {
            @Override
            public Function withWidth(int width) {
                // lift(curry(f, width))
                //
                return Function.from(f.withWidth(width));
            }
            @Override
            public Collection apply(Integer width, Collection cell) {
                // map(cell, f)
                //
                final List r = new ArrayList(cell.size());
                for (String line: cell) { r.add(f.apply(width, line)); }
                return Collections.unmodifiableCollection(r);
            }
        };
    }

    public Function withWidth(final int width) {
        // curry(this, width)
        //
        final FunctionWithWidth f = this;
        return new Function() {
            @Override
            public Collection apply(Collection cell) {
                return f.apply(width, cell);
            }
        };
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy