Skip to content

Commit

Permalink
reuse cached services
Browse files Browse the repository at this point in the history
  • Loading branch information
Vzor- committed Oct 6, 2023
1 parent 274098a commit ddc0141
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/qz/printer/info/CachedPrintService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* See also JDK-7001133
*/
public class CachedPrintService implements PrintService {
private PrintService printService;
private final PrintService printService;
private final long lifespan;
private final CachedObject<String> cachedName;
private final CachedObject<PrintServiceAttributeSet> cachedAttributeSet;
Expand Down Expand Up @@ -62,6 +62,10 @@ public PrintServiceAttributeSet getAttributes() {
return cachedAttributeSet.get();
}

public PrintService getJavaxPrintService() {
return printService;
}

@Override
public <T extends PrintServiceAttribute> T getAttribute(Class<T> category) {
if (!cachedAttributes.containsKey(category)) {
Expand Down
34 changes: 26 additions & 8 deletions src/qz/printer/info/CachedPrintServiceLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
* See also <code>CachedPrintService</code>
*/
public class CachedPrintServiceLookup {
private static final CachedObject<PrintService> cachedDefault = new CachedObject<>(CachedPrintServiceLookup::innerLookupDefaultPrintService);
private static final CachedObject<PrintService[]> cachedPrintServices = new CachedObject<>(CachedPrintServiceLookup::innerLookupPrintServices);
private static final CachedObject<PrintService> cachedDefault = new CachedObject<>(CachedPrintServiceLookup::wrapDefaultPrintService);
private static final CachedObject<PrintService[]> cachedPrintServices = new CachedObject<>(CachedPrintServiceLookup::wrapPrintServices);
private static CachedPrintService[] oldPrintServices = {};

static {
setLifespan(CachedObject.DEFAULT_LIFESPAN);
Expand All @@ -31,15 +32,32 @@ public static PrintService[] lookupPrintServices() {
return cachedPrintServices.get();
}

private static PrintService innerLookupDefaultPrintService() {
private static PrintService wrapDefaultPrintService() {
PrintService javaxPrintService = PrintServiceLookup.lookupDefaultPrintService();
// If this CachedPrintService already exists, reuse it rather than wrapping a new one
CachedPrintService oldCachedPrintService = getMatch(oldPrintServices, javaxPrintService);
if (oldCachedPrintService != null) return oldCachedPrintService;
return new CachedPrintService(PrintServiceLookup.lookupDefaultPrintService());
}

private static PrintService[] innerLookupPrintServices() {
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
for (int i = 0; i < printServices.length; i++) {
printServices[i] = new CachedPrintService(printServices[i]);
private static CachedPrintService[] wrapPrintServices() {
PrintService[] javaxPrintServices = PrintServiceLookup.lookupPrintServices(null, null);
CachedPrintService[] cachedPrintServices = new CachedPrintService[javaxPrintServices.length];
for (int i = 0; i < javaxPrintServices.length; i++) {
// If this CachedPrintService already exists, reuse it rather than wrapping a new one
cachedPrintServices[i] = getMatch(oldPrintServices, javaxPrintServices[i]);
if (cachedPrintServices[i] == null) {
cachedPrintServices[i] = new CachedPrintService(javaxPrintServices[i]);
}
}
return printServices;
oldPrintServices = cachedPrintServices;
return cachedPrintServices;
}

private static CachedPrintService getMatch(CachedPrintService[] array, PrintService javaxPrintService) {
for (CachedPrintService cps : array) {
if (cps.getJavaxPrintService() == javaxPrintService) return cps;
}
return null;
}
}

0 comments on commit ddc0141

Please sign in to comment.