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

org.sejda.model.parameter.RotateParameters Maven / Gradle / Ivy

/*
 * Created on 30/mag/2010
 *
 * Copyright 2010 by Andrea Vacondio ([email protected]).
 * 
 * This file is part of the Sejda source code
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package org.sejda.model.parameter;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.sejda.model.parameter.base.MultiplePdfSourceMultipleOutputParameters;
import org.sejda.model.pdf.page.PageRange;
import org.sejda.model.pdf.page.PredefinedSetOfPages;
import org.sejda.model.rotation.Rotation;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Parameter class for the rotation manipulation.
 *
 * Accepts a list of {@link org.sejda.model.input.PdfSource}
 *
 * Can specify a rotation to be applied to a predefined set of pages (all, even, odd), all sources.
 * Can specify a rotation to be applied per page range, all sources.
 * Can specify a rotation to be applied per page range, per source.
 * 
 * @author Andrea Vacondio
 * @author Edi Weissmann
 * 
 */
public class RotateParameters extends MultiplePdfSourceMultipleOutputParameters {

    @Valid
    @NotNull
    // same rotation all pages, all sources
    private Rotation rotation = null;

    @NotNull
    // same rotation per page set, all sources
    private PredefinedSetOfPages predefinedSetOfPages;

    @Valid
    // different rotations per page, all sources
    private final Map pageSelection = new HashMap<>();

    // different rotations per page, per source
    private Map> pageSelectionPerSource = new HashMap<>();

    public RotateParameters(Rotation rotation, PredefinedSetOfPages predefinedSetOfPages) {
        this.rotation = rotation;
        this.predefinedSetOfPages = predefinedSetOfPages;
    }

    public RotateParameters(Rotation rotation) {
        this(rotation, PredefinedSetOfPages.NONE);
    }

    public RotateParameters() {
        this(Rotation.DEGREES_0);
    }

    @Deprecated
    public Rotation getRotation() {
        return rotation;
    }

    public Rotation getRotation(int page) {
        Rotation defaultRotation = Rotation.DEGREES_0;
        if(predefinedSetOfPages.includes(page)) {
            defaultRotation = rotation;
        }

        return pageSelection.keySet().stream().filter(range -> range.contains(page)).findFirst().map(pageSelection::get)
                .orElse(defaultRotation);
    }

    public Rotation getRotation(int sourceIndex, int page) {
        Map pageSelection = pageSelectionPerSource.get(sourceIndex);
        if(pageSelection != null) {
            for(PageRange range: pageSelection.keySet()) {
                if(range.contains(page)) {
                    return pageSelection.get(range);
                }
            }
        }

        // no source specific rotation defined for page
        return getRotation(page);
    }

    public void addPageRange(PageRange range) {
        pageSelection.put(range, this.rotation);
    }

    public void addPageRange(PageRange range, Rotation rotation) {
        pageSelection.put(range, rotation);
    }

    public void addAllPageRanges(Collection ranges) {
        ranges.forEach(this::addPageRange);
    }

    public void addPageRangePerSource(int sourceIndex, PageRange range, Rotation rotation) {
        if(!pageSelectionPerSource.containsKey(sourceIndex)) {
            pageSelectionPerSource.put(sourceIndex, new HashMap<>());
        }

        Map pageSelection = pageSelectionPerSource.get(sourceIndex);
        pageSelection.put(range, rotation);
    }

    public PredefinedSetOfPages getPredefinedSetOfPages() {
        return predefinedSetOfPages;
    }

    public Map getPageSelection() {
        return pageSelection;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().appendSuper(super.hashCode()).append(rotation).append(predefinedSetOfPages)
                .append(pageSelection).append(pageSelectionPerSource).toHashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof RotateParameters parameter)) {
            return false;
        }
        return new EqualsBuilder().appendSuper(super.equals(other))
                .append(predefinedSetOfPages, parameter.predefinedSetOfPages)
                .append(pageSelection, parameter.pageSelection)
                .append(rotation, parameter.rotation)
                .append(pageSelectionPerSource, parameter.pageSelectionPerSource)
                .isEquals();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy