Skip to content

Commit

Permalink
Added a couple of utility methods to PageSize. Brought equals and toS…
Browse files Browse the repository at this point in the history
…tring in line with other model classes
  • Loading branch information
torakiki committed Dec 2, 2024
1 parent 83d2e03 commit 705010c
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 28 deletions.
81 changes: 53 additions & 28 deletions sejda-model/src/main/java/org/sejda/model/PageSize.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class PageSize {

private float width;
private float height;
private String name;
private final float width;
private final float height;
private final String name;

/** user space units per inch */
private static final float POINTS_PER_INCH = 72;
Expand Down Expand Up @@ -83,38 +85,65 @@ public float getHeight() {
return height;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
/**
* @return a new {@link PageSize} instance with dimensions rotated.
*/
public PageSize rotate() {
return new PageSize(height, width);
}

public boolean isLandscape() {
return getWidth() > getHeight();
}

if (o == null || getClass() != o.getClass()) return false;
/**
* @return a new {@link PageSize} instance with dimensions rotated if the current instance
* is in landscape orientation, otherwise returns the current instance.
*/
public PageSize toPortrait() {
if (isLandscape()) {
return rotate();
}
return this;
}

PageSize pageSize = (PageSize) o;
/**
* @return a new {@link PageSize} instance with dimensions rotated if the current instance
* is not in landscape orientation, otherwise returns the current instance.
*/
public PageSize toLandscape() {
if (!isLandscape()) {
return rotate();
}
return this;
}

return new EqualsBuilder()
.append(width, pageSize.width)
.append(height, pageSize.height)
.isEquals();
public String getName() {
return name;
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(width)
.append(height)
.toHashCode();
public boolean equals(Object other) {
if (this == other) {
return true;
}

if (!(other instanceof PageSize instance)) {
return false;
}

return new EqualsBuilder().append(width, instance.width).append(height, instance.height).isEquals();
}

@Override
public String toString() {
return "PageSize{" +
"widthInches=" + width +
", heightInches=" + height +
'}';
public int hashCode() {
return new HashCodeBuilder(17, 37).append(width).append(height).toHashCode();
}

public PageSize rotate() {
return new PageSize(height, width);
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("widthInches", width)
.append("heightInches", height).append("widthInches", width).append("height", height).toString();
}

public static PageSize fromInches(float widthInch, float heightInch) {
Expand All @@ -124,8 +153,4 @@ public static PageSize fromInches(float widthInch, float heightInch) {
public static PageSize fromMillimeters(float widthInch, float heightInch) {
return new PageSize(widthInch * POINTS_PER_MM, heightInch * POINTS_PER_MM);
}

public String getName() {
return name;
}
}
61 changes: 61 additions & 0 deletions sejda-model/src/test/java/org/sejda/model/PageSizeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.sejda.model;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Test class for {@link PageSize}.
* <p>
* This class tests the functionality of the `rotate` method in `PageSize` class, which swaps the width and height,
* effectively changing the page's orientation between portrait and landscape.
*/
public class PageSizeTest {

@Test
public void testToPortraitFromLandscape() {
var landscape = new PageSize(200, 100, "Landscape");
var result = landscape.toPortrait();

assertFalse(result.isLandscape());
assertEquals(100, result.getWidth());
assertEquals(200, result.getHeight());
assertNotEquals(landscape, result);
}

@Test
public void testToPortraitFromPortrait() {
var portrait = new PageSize(100, 200, "Portrait");
var result = portrait.toPortrait();

assertFalse(result.isLandscape());
assertEquals(100, result.getWidth());
assertEquals(200, result.getHeight());
assertEquals(portrait, result);
}

@Test
public void testToLandscapeFromPortrait() {
var portrait = new PageSize(100, 200, "Portrait");
var result = portrait.toLandscape();

assertTrue(result.isLandscape());
assertEquals(200, result.getWidth());
assertEquals(100, result.getHeight());
assertNotEquals(portrait, result);
}

@Test
public void testToLandscapeFromLandscape() {
var landscape = new PageSize(200, 100, "Landscape");
var result = landscape.toLandscape();

assertTrue(result.isLandscape());
assertEquals(200, result.getWidth());
assertEquals(100, result.getHeight());
assertEquals(landscape, result);
}
}

0 comments on commit 705010c

Please sign in to comment.