21
21
import java .util .Map ;
22
22
23
23
public class PageSize {
24
-
25
24
private final double height ;
26
25
private final double width ;
27
26
28
- // Default Constructor (A4 by default)
29
- public PageSize () {
30
- this (PaperSize .A4 ); // Delegate to predefined size constructor
31
- }
27
+ // Predefined constants
28
+ public static final PageSize A4 = new PageSize (27.94 , 21.59 );
29
+ public static final PageSize LEGAL = new PageSize (35.56 , 21.59 );
30
+ public static final PageSize TABLOID = new PageSize (43.18 , 27.94 );
31
+ public static final PageSize LETTER = new PageSize (27.94 , 21.59 );
32
32
33
- // Custom Size Constructor
34
33
public PageSize (double height , double width ) {
35
34
this .height = height ;
36
35
this .width = width ;
37
36
}
38
37
39
- // Constructor for Predefined Sizes A4,A6,LEGAL,TABLOID using PaperSize Enum
40
- public PageSize (PaperSize paperSize ) {
41
- this (paperSize .getHeight (), paperSize .getWidth ()); // Delegate to custom size constructor
42
- }
43
-
44
- // Factory Methods for Predefined Sizes
45
- public static PageSize A4 () {
46
- return new PageSize (PaperSize .A4 );
47
- }
48
-
49
- public static PageSize A6 () {
50
- return new PageSize (PaperSize .A6 );
51
- }
52
-
53
- public static PageSize LEGAL () {
54
- return new PageSize (PaperSize .LEGAL );
55
- }
56
-
57
- public static PageSize TABLOID () {
58
- return new PageSize (PaperSize .TABLOID );
38
+ // Default constructor (e.g., default to A4)
39
+ public PageSize () {
40
+ this (A4 .getHeight (), A4 .getWidth ());
59
41
}
60
42
61
43
// Getters for Height and Width
@@ -67,41 +49,16 @@ public double getWidth() {
67
49
return width ;
68
50
}
69
51
70
- // Convert to Map (for serialization or configuration)
71
52
public Map <String , Object > toMap () {
72
- final Map <String , Object > options = new HashMap <>();
73
- options .put ("height" , getHeight ());
74
- options .put ("width" , getWidth ());
75
- return options ;
76
- }
77
-
78
- // Enum for Predefined Sizes
79
- public enum PaperSize {
80
- A4 (27.94 , 21.59 ),
81
- A6 (14.8 , 10.5 ),
82
- LEGAL (35.56 , 21.59 ),
83
- TABLOID (43.18 , 27.94 );
84
-
85
- private final double height ;
86
- private final double width ;
87
-
88
- PaperSize (double height , double width ) {
89
- this .height = height ;
90
- this .width = width ;
91
- }
92
-
93
- public double getHeight () {
94
- return height ;
95
- }
96
-
97
- public double getWidth () {
98
- return width ;
99
- }
53
+ Map <String , Object > map = new HashMap <>();
54
+ map .put ("width" , width );
55
+ map .put ("height" , height );
56
+ return map ;
100
57
}
101
58
102
59
@ Override
103
60
public String toString () {
104
- return String . format ( "PageSize[height=%.2f, width=%.2f] " , height , width ) ;
61
+ return "PageSize[width=" + this . getWidth () + ", height=" + this . getHeight () + "]" ;
105
62
}
106
63
107
64
}
0 commit comments