diff --git a/src/main/java/net/kemitix/fontface/FontCache.java b/src/main/java/net/kemitix/fontface/FontCache.java index afdacb7..3542931 100644 --- a/src/main/java/net/kemitix/fontface/FontCache.java +++ b/src/main/java/net/kemitix/fontface/FontCache.java @@ -1,9 +1,73 @@ package net.kemitix.fontface; -import net.kemitix.fontface.FontFace; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.RequiredArgsConstructor; import java.awt.*; +import java.net.URI; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; +import java.util.logging.Logger; -public interface FontCache { - Font loadFont(FontFace fontFace); +@RequiredArgsConstructor +public class FontCache { + + private static final Logger LOGGER = + Logger.getLogger( + FontCache.class.getName()); + + private final Map fileCache = new HashMap<>(); + + private final Map fontCache = new HashMap<>(); + + private final FontLoader fontLoader; + + public Font loadFont(final FontFace fontFace) { + LOGGER.finest(String.format("Requesting %s %d", + fontFace.getFontLocation(), fontFace.getSize())); + final Font baseFont = + fileCache.computeIfAbsent( + fontFace.getFontLocation(), + loadNewFontFile(fontFace)); + return fontCache.computeIfAbsent( + FontAndSize.of(baseFont, fontFace.getSize()), + resizeFont()); + } + + private Function loadNewFontFile( + final FontFace fontFace + ) { + return uri -> { + LOGGER.fine(String.format("Loading %s", uri)); + return fontLoader.loadFont(fontFace); + }; + } + + + private Function resizeFont() { + return fontAndSize -> { + LOGGER.finer(String.format("Resizing %s to %d", + fontAndSize.getFont().getName(), fontAndSize.getSize())); + return fontAndSize.font + .deriveFont(Font.PLAIN, fontAndSize.getSize()); + }; + } + + @Getter + @EqualsAndHashCode + private static class FontAndSize { + private final Font font; + private final int size; + + private FontAndSize(final Font font, final int size) { + this.font = font; + this.size = size; + } + + static FontAndSize of(final Font font, final int size) { + return new FontAndSize(font, size); + } + } } diff --git a/src/main/java/net/kemitix/fontface/FontCacheImpl.java b/src/main/java/net/kemitix/fontface/FontCacheImpl.java deleted file mode 100644 index 1133843..0000000 --- a/src/main/java/net/kemitix/fontface/FontCacheImpl.java +++ /dev/null @@ -1,74 +0,0 @@ -package net.kemitix.fontface; - -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -import java.awt.*; -import java.net.URI; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Function; -import java.util.logging.Logger; - -@RequiredArgsConstructor -public class FontCacheImpl implements FontCache { - - private static final Logger LOGGER = - Logger.getLogger( - FontCacheImpl.class.getName()); - - private final Map fileCache = new HashMap<>(); - - private final Map fontCache = new HashMap<>(); - - private final FontLoader fontLoader; - - @Override - public Font loadFont(final FontFace fontFace) { - LOGGER.finest(String.format("Requesting %s %d", - fontFace.getFontLocation(), fontFace.getSize())); - final Font baseFont = - fileCache.computeIfAbsent( - fontFace.getFontLocation(), - loadNewFontFile(fontFace)); - return fontCache.computeIfAbsent( - FontAndSize.of(baseFont, fontFace.getSize()), - resizeFont()); - } - - private Function loadNewFontFile( - final FontFace fontFace - ) { - return uri -> { - LOGGER.fine(String.format("Loading %s", uri)); - return fontLoader.loadFont(fontFace); - }; - } - - - private Function resizeFont() { - return fontAndSize -> { - LOGGER.finer(String.format("Resizing %s to %d", - fontAndSize.getFont().getName(), fontAndSize.getSize())); - return fontAndSize.font - .deriveFont(Font.PLAIN, fontAndSize.getSize()); - }; - } - - @Getter - @EqualsAndHashCode - private static class FontAndSize { - private final Font font; - private final int size; - - private FontAndSize(final Font font, final int size) { - this.font = font; - this.size = size; - } - - static FontAndSize of(final Font font, final int size) { - return new FontAndSize(font, size); - } - } -} diff --git a/src/main/test/java/net/kemitix/fontface/FontCacheImplTest.java b/src/main/test/java/net/kemitix/fontface/FontCacheTest.java similarity index 89% rename from src/main/test/java/net/kemitix/fontface/FontCacheImplTest.java rename to src/main/test/java/net/kemitix/fontface/FontCacheTest.java index a1cc9da..64ec0f2 100644 --- a/src/main/test/java/net/kemitix/fontface/FontCacheImplTest.java +++ b/src/main/test/java/net/kemitix/fontface/FontCacheTest.java @@ -18,7 +18,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @ExtendWith(MockitoExtension.class) -public class FontCacheImplTest { +public class FontCacheTest { private final URI fontLocation = new File("font.otf").toURI(); private final String colour = "colour"; @@ -36,7 +36,7 @@ public class FontCacheImplTest { @DisplayName("When cache is empty then load font") public void emptyCache() { //given - final FontCacheImpl fontCache = new FontCacheImpl(fontLoader); + final FontCache fontCache = new FontCache(fontLoader); final FontFace fontFace = FontFace.of(fontLocation, 26, colour); //when fontCache.loadFont(fontFace); @@ -48,7 +48,7 @@ public class FontCacheImplTest { @DisplayName("When cache has font, but not size, then don't reload font") public void notInSize() { //given - final FontCacheImpl fontCache = new FontCacheImpl(fontLoader); + final FontCache fontCache = new FontCache(fontLoader); final FontFace previousFontFace = FontFace.of(fontLocation, 16, colour); fontCache.loadFont(previousFontFace); final FontFace fontFace = FontFace.of(fontLocation, 26, colour); @@ -63,7 +63,7 @@ public class FontCacheImplTest { @DisplayName("When cache has font in size, then don't load font") public void available() { //given - final FontCacheImpl fontCache = new FontCacheImpl(fontLoader); + final FontCache fontCache = new FontCache(fontLoader); final FontFace fontFace = FontFace.of(fontLocation, 26, colour); //when fontCache.loadFont(fontFace);