com.helger.pdflayout.spec.WidthSpec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ph-pdf-layout Show documentation
Show all versions of ph-pdf-layout Show documentation
Library for creating nicely layouted PDF documents based on PDFBox
/**
* Copyright (C) 2014-2016 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* 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 com.helger.pdflayout.spec;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.MustImplementEqualsAndHashcode;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.equals.EqualsHelper;
import com.helger.commons.hashcode.HashCodeGenerator;
import com.helger.commons.id.IHasID;
import com.helger.commons.lang.EnumHelper;
import com.helger.commons.string.ToStringGenerator;
/**
* This class defines a dependent width of an elements:
*
* - absolute - element has a fixed width
* - percentage - element width is a certain percentage of the surrounding
* element
* - star - element width is a relative part of the unused width of the
* surrounding element
*
*
* @author Philip Helger
*/
@Immutable
@MustImplementEqualsAndHashcode
public class WidthSpec
{
/**
* Defines the type of width unit of measure used.
*
* @author Philip Helger
*/
public static enum EWidthType implements IHasID
{
/** Absolute value provided */
ABSOLUTE ("abs"),
/** Percentage value provided */
PERCENTAGE ("perc"),
/** * value provided */
STAR ("star");
private final String m_sID;
private EWidthType (@Nonnull @Nonempty final String sID)
{
m_sID = sID;
}
@Nonnull
@Nonempty
public String getID ()
{
return m_sID;
}
@Nullable
public static EWidthType getFromIDOrNull (@Nullable final String sID)
{
return EnumHelper.getFromIDOrNull (EWidthType.class, sID);
}
}
private final EWidthType m_eType;
private final float m_fValue;
public WidthSpec (@Nonnull final EWidthType eType, final float fValue)
{
ValueEnforcer.notNull (eType, "WidthType");
m_eType = eType;
m_fValue = fValue;
}
/**
* @return The width type. Never null
.
*/
@Nonnull
public EWidthType getType ()
{
return m_eType;
}
/**
* @return The ID of the width type. Never null
.
*/
@Nonnull
@Nonempty
public String getTypeID ()
{
return m_eType.getID ();
}
/**
* @return true
if type is 'star'.
*/
public boolean isStar ()
{
return m_eType == EWidthType.STAR;
}
/**
* @return The width value - is either an absolute value or a percentage value
* - depending on {@link #getType()}. For star width elements this is
* 0.
*/
@Nonnegative
public float getValue ()
{
return m_fValue;
}
/**
* Get the effective width based on the passed available width. This may not
* be called for star width elements.
*
* @param fAvailableWidth
* The available width.
* @return The effective width to use.
*/
@Nonnegative
public float getEffectiveValue (final float fAvailableWidth)
{
switch (m_eType)
{
case ABSOLUTE:
return Math.min (m_fValue, fAvailableWidth);
case PERCENTAGE:
return fAvailableWidth * m_fValue / 100;
default:
throw new IllegalStateException ("Unsupported: " + m_eType + " - must be calculated outside!");
}
}
@Override
public boolean equals (final Object o)
{
if (this == o)
return true;
if (o == null || !getClass ().equals (o.getClass ()))
return false;
final WidthSpec rhs = (WidthSpec) o;
return m_eType.equals (rhs.m_eType) && EqualsHelper.equals (m_fValue, rhs.m_fValue);
}
@Override
public int hashCode ()
{
return new HashCodeGenerator (this).append (m_eType).append (m_fValue).getHashCode ();
}
@Override
public String toString ()
{
return new ToStringGenerator (this).append ("type", m_eType).append ("value", m_fValue).toString ();
}
/**
* Create a width element with an absolute value.
*
* @param fValue
* The width to use. Must be > 0.
* @return Never null
.
*/
@Nonnull
public static WidthSpec abs (@Nonnegative final float fValue)
{
ValueEnforcer.isGT0 (fValue, "Value");
return new WidthSpec (EWidthType.ABSOLUTE, fValue);
}
/**
* Create a width element with an percentage value.
*
* @param fPerc
* The width percentage to use. Must be > 0.
* @return Never null
.
*/
@Nonnull
public static WidthSpec perc (@Nonnegative final float fPerc)
{
ValueEnforcer.isGT0 (fPerc, "Perc");
return new WidthSpec (EWidthType.PERCENTAGE, fPerc);
}
/**
* Create a new star width element.
*
* @return Never null
.
*/
@Nonnull
public static WidthSpec star ()
{
return new WidthSpec (EWidthType.STAR, 0);
}
}