Skip to content

Commit

Permalink
[refactor] convert class PageElementPosition to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Oct 12, 2024
1 parent 95a9132 commit 19a47b5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,37 @@
*/
package org.xhtmlrenderer.css.constants;

import java.util.HashMap;
import java.util.Map;
import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;

public class PageElementPosition {
private static final Map<String, PageElementPosition> ALL = new HashMap<>();
private static int _maxAssigned;
import java.util.Map;

public final int FS_ID;
public enum PageElementPosition {
START("start"),
FIRST("first"),
LAST("last"),
LAST_EXCEPT("last-except");

private static final Map<String, PageElementPosition> ALL = Map.of(
START._ident, START,
FIRST._ident, FIRST,
LAST._ident, LAST,
LAST_EXCEPT._ident, LAST_EXCEPT
);

@Nullable
@CheckReturnValue
public static PageElementPosition byIdent(String ident) {
return ALL.get(ident);
}

private final String _ident;

public static final PageElementPosition START = addValue("start");
public static final PageElementPosition FIRST = addValue("first");
public static final PageElementPosition LAST = addValue("last");
public static final PageElementPosition LAST_EXCEPT = addValue("last-except");

private PageElementPosition(String ident) {
PageElementPosition(String ident) {
this._ident = ident;
this.FS_ID = _maxAssigned++;
}

private static PageElementPosition addValue(String ident) {
PageElementPosition val = new PageElementPosition(ident);
ALL.put(ident, val);
return val;
}

public String toString() {
return _ident;
}

public static PageElementPosition valueOf(String ident) {
return ALL.get(ident);
}

public int hashCode() {
return FS_ID;
}

public boolean equals(Object o) {
if (!(o instanceof PageElementPosition)) {
return false;
}

return FS_ID == ((PageElementPosition)o).FS_ID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ public static BlockBox getRunningBlock(LayoutContext c, PropertyValue value) {
String ident = params.get(0).getStringValue();
PageElementPosition position = null;
if (params.size() == 2) {
position = PageElementPosition.valueOf(params.get(1).getStringValue());
position = PageElementPosition.byIdent(params.get(1).getStringValue());
}
if (position == null) {
position = PageElementPosition.FIRST;
Expand Down

0 comments on commit 19a47b5

Please sign in to comment.