Skip to content

Commit

Permalink
Merge pull request LibrePDF#48 from otep/master
Browse files Browse the repository at this point in the history
Generified some collections where easily applicable.
  • Loading branch information
asturio authored Oct 31, 2017
2 parents 5d4a922 + da0a529 commit c344c23
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 125 deletions.
20 changes: 8 additions & 12 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -102,7 +103,7 @@ public class PdfDictionary extends PdfObject {
private PdfName dictionaryType = null;

/** This is the hashmap that contains all the values and keys of the dictionary */
protected HashMap hashMap;
protected Map<PdfName, PdfObject> hashMap;

// CONSTRUCTORS

Expand All @@ -111,7 +112,7 @@ public class PdfDictionary extends PdfObject {
*/
public PdfDictionary() {
super(DICTIONARY);
hashMap = new HashMap();
hashMap = new HashMap<PdfName, PdfObject>();
}

/**
Expand Down Expand Up @@ -139,14 +140,10 @@ public void toPdf(PdfWriter writer, OutputStream os) throws IOException {
os.write('<');
os.write('<');
// loop over all the object-pairs in the HashMap
PdfName key;
PdfObject value;
int type = 0;
for (Iterator i = hashMap.keySet().iterator(); i.hasNext(); ) {
key = (PdfName) i.next();
value = (PdfObject) hashMap.get(key);
key.toPdf(writer, os);
type = value.type();
for (PdfName pdfName : hashMap.keySet()) {
PdfObject value = hashMap.get(pdfName);
pdfName.toPdf(writer, os);
int type = value.type();
if (type != PdfObject.ARRAY && type != PdfObject.DICTIONARY && type != PdfObject.NAME && type != PdfObject.STRING)
os.write(' ');
value.toPdf(writer, os);
Expand Down Expand Up @@ -345,8 +342,7 @@ public void merge(PdfDictionary other) {
}

public void mergeDifferent(PdfDictionary other) {
for (Iterator i = other.hashMap.keySet().iterator(); i.hasNext();) {
Object key = i.next();
for (PdfName key : other.hashMap.keySet()) {
if (!hashMap.containsKey(key))
hashMap.put(key, other.hashMap.get(key));
}
Expand Down
69 changes: 36 additions & 33 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,17 @@ static class PdfCatalog extends PdfDictionary {
* @param documentFileAttachment the attached files
* @param writer the writer the catalog applies to
*/
void addNames(TreeMap localDestinations, HashMap documentLevelJS, HashMap documentFileAttachment, PdfWriter writer) {
void addNames(TreeMap<String, Object[]> localDestinations, HashMap<String, PdfIndirectReference> documentLevelJS, HashMap<String, PdfIndirectReference> documentFileAttachment, PdfWriter writer) {
if (localDestinations.isEmpty() && documentLevelJS.isEmpty() && documentFileAttachment.isEmpty())
return;
try {
PdfDictionary names = new PdfDictionary();
if (!localDestinations.isEmpty()) {
PdfArray ar = new PdfArray();
for (Iterator i = localDestinations.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
String name = (String) entry.getKey();
Object obj[] = (Object[]) entry.getValue();
for (Iterator<Map.Entry<String, Object[]>> i = localDestinations.entrySet().iterator(); i.hasNext();) {
Map.Entry<String, Object[]> entry = i.next();
String name = entry.getKey();
Object obj[] = entry.getValue();
if (obj[2] == null) //no destination
continue;
PdfIndirectReference ref = (PdfIndirectReference)obj[1];
Expand Down Expand Up @@ -1161,7 +1161,7 @@ protected void initPage() throws DocumentException {
currentHeight = 0;

// backgroundcolors, etc...
thisBoxSize = new HashMap(boxSize);
thisBoxSize = new HashMap<String, PdfRectangle>(boxSize);
if (pageSize.getBackgroundColor() != null
|| pageSize.hasBorders()
|| pageSize.getBorderColor() != null) {
Expand Down Expand Up @@ -1202,8 +1202,9 @@ protected void initPage() throws DocumentException {

/** The line that is currently being written. */
protected PdfLine line = null;

/** The lines that are written until now. */
protected ArrayList lines = new ArrayList();
protected ArrayList<PdfLine> lines = new ArrayList<>();

/**
* Adds the current line to the list of lines and also adds an empty line.
Expand All @@ -1226,7 +1227,7 @@ protected void newLine() throws DocumentException {
protected void carriageReturn() {
// the arraylist with lines may not be null
if (lines == null) {
lines = new ArrayList();
lines = new ArrayList<>();
}
// If the current line is not null
if (line != null) {
Expand Down Expand Up @@ -2062,11 +2063,11 @@ void setAction(PdfAction action, float llx, float lly, float urx, float ury) {
* Stores the destinations keyed by name. Value is
* <CODE>Object[]{PdfAction,PdfIndirectReference,PdfDestintion}</CODE>.
*/
protected TreeMap localDestinations = new TreeMap();
protected TreeMap<String, Object[]> localDestinations = new TreeMap<>();

PdfAction getLocalGotoAction(String name) {
PdfAction action;
Object obj[] = (Object[])localDestinations.get(name);
Object obj[] = localDestinations.get(name);
if (obj == null)
obj = new Object[3];
if (obj[0] == null) {
Expand All @@ -2093,7 +2094,7 @@ PdfAction getLocalGotoAction(String name) {
* already existed
*/
boolean localDestination(String name, PdfDestination destination) {
Object obj[] = (Object[])localDestinations.get(name);
Object obj[] = localDestinations.get(name);
if (obj == null)
obj = new Object[3];
if (obj[2] != null)
Expand All @@ -2109,7 +2110,7 @@ boolean localDestination(String name, PdfDestination destination) {
* Stores a list of document level JavaScript actions.
*/
int jsCounter;
protected HashMap documentLevelJS = new HashMap();
protected HashMap<String, PdfIndirectReference> documentLevelJS = new HashMap<String, PdfIndirectReference>();
protected static final DecimalFormat SIXTEEN_DIGITS = new DecimalFormat("0000000000000000");
void addJavaScript(PdfAction js) {
if (js.get(PdfName.JS) == null)
Expand All @@ -2132,11 +2133,11 @@ void addJavaScript(String name, PdfAction js) {
}
}

HashMap getDocumentLevelJS() {
HashMap<String, PdfIndirectReference> getDocumentLevelJS() {
return documentLevelJS;
}

protected HashMap documentFileAttachment = new HashMap();
protected HashMap<String, PdfIndirectReference> documentFileAttachment = new HashMap<String, PdfIndirectReference>();

void addFileAttachment(String description, PdfFileSpecification fs) throws IOException {
if (description == null) {
Expand All @@ -2160,7 +2161,7 @@ void addFileAttachment(String description, PdfFileSpecification fs) throws IOExc
documentFileAttachment.put(fn, fs.getReference());
}

HashMap getDocumentFileAttachment() {
HashMap<String, PdfIndirectReference> getDocumentFileAttachment() {
return documentFileAttachment;
}

Expand Down Expand Up @@ -2247,11 +2248,11 @@ void incMarkPoint() {
protected Rectangle nextPageSize = null;

/** This is the size of the several boxes of the current Page. */
protected HashMap thisBoxSize = new HashMap();
protected HashMap<String, PdfRectangle> thisBoxSize = new HashMap<String, PdfRectangle>();

/** This is the size of the several boxes that will be used in
* the next page. */
protected HashMap boxSize = new HashMap();
protected HashMap<String, PdfRectangle> boxSize = new HashMap<String, PdfRectangle>();

void setCropBoxSize(Rectangle crop) {
setBoxSize("crop", crop);
Expand Down Expand Up @@ -2289,7 +2290,7 @@ protected void setNewPageSizeAndMargins() {
* @param boxName crop, trim, art or bleed
*/
Rectangle getBoxSize(String boxName) {
PdfRectangle r = (PdfRectangle)thisBoxSize.get(boxName);
PdfRectangle r = thisBoxSize.get(boxName);
if (r != null) return r.getRectangle();
return null;
}
Expand Down Expand Up @@ -2551,8 +2552,11 @@ protected static class RenderingContext {
float maxCellBottom;
float maxCellHeight;

Map rowspanMap;
Map pageMap = new HashMap();
Map<PdfCell, Integer> rowspanMap = new HashMap<>();

// Possible keys and values are Set or Integer. Really?
Map<Object, Object> pageMap = new HashMap<>();

/**
* A PdfPTable
*/
Expand All @@ -2568,7 +2572,7 @@ public int consumeRowspan(PdfCell c) {
return 1;
}

Integer i = (Integer) rowspanMap.get(c);
Integer i = rowspanMap.get(c);
if (i == null) {
i = new Integer(c.rowspan());
}
Expand All @@ -2588,7 +2592,7 @@ public int consumeRowspan(PdfCell c) {
* @return the current rowspan
*/
public int currentRowspan(PdfCell c) {
Integer i = (Integer) rowspanMap.get(c);
Integer i = rowspanMap.get(c);
if (i == null) {
return c.rowspan();
} else {
Expand All @@ -2599,13 +2603,13 @@ public int currentRowspan(PdfCell c) {
public int cellRendered(PdfCell cell, int pageNumber) {
Integer i = (Integer) pageMap.get(cell);
if (i == null) {
i = new Integer(1);
i = 1;
} else {
i = new Integer(i.intValue() + 1);
i = i + 1;
}
pageMap.put(cell, i);

Integer pageInteger = new Integer(pageNumber);
Integer pageInteger = pageNumber;
Set set = (Set) pageMap.get(pageInteger);

if (set == null) {
Expand All @@ -2615,19 +2619,19 @@ public int cellRendered(PdfCell cell, int pageNumber) {

set.add(cell);

return i.intValue();
return i;
}

public int numCellRendered(PdfCell cell) {
Integer i = (Integer) pageMap.get(cell);
if (i == null) {
i = new Integer(0);
i = 0;
}
return i.intValue();
return i;
}

public boolean isCellRenderedOnPage(PdfCell cell, int pageNumber) {
Integer pageInteger = new Integer(pageNumber);
Integer pageInteger = pageNumber;
Set set = (Set) pageMap.get(pageInteger);

if (set != null) {
Expand All @@ -2653,7 +2657,7 @@ private void addPdfTable(Table t) throws DocumentException {
ctx.pagetop = indentTop();
ctx.oldHeight = currentHeight;
ctx.cellGraphics = new PdfContentByte(writer);
ctx.rowspanMap = new HashMap();
ctx.rowspanMap = new HashMap<>();
ctx.table = table;

// initialization of parameters
Expand Down Expand Up @@ -2690,7 +2694,7 @@ private void addPdfTable(Table t) throws DocumentException {

// compose cells array list for subsequent code
cells.clear();
Set opt = new HashSet();
Set<PdfCell> opt = new HashSet<>();
iterator = rows.iterator();
while (iterator.hasNext()) {
ArrayList row = (ArrayList) iterator.next();
Expand Down Expand Up @@ -2914,7 +2918,7 @@ protected ArrayList extractRows(ArrayList cells, RenderingContext ctx) {
PdfCell cell;
PdfCell previousCell = null;
ArrayList rows = new ArrayList();
java.util.List rowCells = new ArrayList();
java.util.List<PdfCell> rowCells = new ArrayList<>();

Iterator iterator = cells.iterator();
while (iterator.hasNext()) {
Expand All @@ -2939,7 +2943,6 @@ protected ArrayList extractRows(ArrayList cells, RenderingContext ctx) {

if (isEndOfRow) {
if (!rowCells.isEmpty()) {
// add to rowlist
rows.add(rowCells);
}

Expand Down
6 changes: 3 additions & 3 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
*/
public class PdfLayer extends PdfDictionary implements PdfOCG {
protected PdfIndirectReference ref;
protected ArrayList children;
protected ArrayList<PdfLayer> children;
protected PdfLayer parent;
protected String title;

Expand Down Expand Up @@ -115,7 +115,7 @@ public void addChild(PdfLayer child) {
throw new IllegalArgumentException(MessageLocalization.getComposedMessage("the.layer.1.already.has.a.parent", ((PdfString)child.get(PdfName.NAME)).toUnicodeString()));
child.parent = this;
if (children == null)
children = new ArrayList();
children = new ArrayList<>();
children.add(child);
}

Expand All @@ -132,7 +132,7 @@ public PdfLayer getParent() {
* Gets the children layers.
* @return the children layers or <CODE>null</CODE> if the layer has no children
*/
public ArrayList getChildren() {
public ArrayList<PdfLayer> getChildren() {
return children;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
* Content typically belongs to a single optional content group,
Expand Down Expand Up @@ -80,7 +81,7 @@ public class PdfLayerMembership extends PdfDictionary implements PdfOCG {

PdfIndirectReference ref;
PdfArray members = new PdfArray();
HashSet layers = new HashSet();
Set<PdfLayer> layers = new HashSet<>();

/**
* Creates a new, empty, membership layer.
Expand Down
Loading

0 comments on commit c344c23

Please sign in to comment.