# HG changeset patch # User Jaroslav Tulach # Date 1230229559 -3600 # Node ID dbe255defdbb26485ecad9cead997cafd60ba4d3 # Parent b704be5f8463a75d4beea9b40fdba19586c2522a Adding day.end.bridges.DigestImpl code snippet diff -r b704be5f8463 -r dbe255defdbb samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java --- a/samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java Sat Nov 15 13:12:46 2008 +0100 +++ b/samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java Thu Dec 25 19:25:59 2008 +0100 @@ -12,10 +12,10 @@ */ // BEGIN: day.end.bridges.Digest public final class Digest { - private final DigestImplementation impl; + private final DigestImpl impl; /** Factory method is better than constructor */ - private Digest(DigestImplementation impl) { + private Digest(DigestImpl impl) { this.impl = impl; } @@ -23,7 +23,7 @@ */ public static Digest getInstance(String algorithm) { for (Digestor digestor : ServiceLoader.load(Digestor.class)) { - DigestImplementation impl = DigestImplementation.create( + DigestImpl impl = DigestImpl.create( digestor, algorithm ); if (impl != null) { diff -r b704be5f8463 -r dbe255defdbb samples/messagedigest/src-new-api/org/apidesign/api/security/DigestImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/messagedigest/src-new-api/org/apidesign/api/security/DigestImpl.java Thu Dec 25 19:25:59 2008 +0100 @@ -0,0 +1,46 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.apidesign.api.security; + +import java.nio.ByteBuffer; +import org.apidesign.spi.security.Digestor; + +/** + * + * @author Jaroslav Tulach + */ +// BEGIN: day.end.bridges.DigestImpl +final class DigestImpl { + private static final DigestorAccessorImpl ACCESSOR = new DigestorAccessorImpl(); + + private final Digestor digestor; + private final String algorithm; + private Data data; + + private DigestImpl(Digestor digestor, String algorithm, Data d) { + this.digestor = digestor; + this.algorithm = algorithm; + this.data = d; + } + + static DigestImpl create(Digestor digestor, String algorithm) { + // indirectly calls digestor.create(algorithm) + Data d = ACCESSOR.create(digestor, algorithm); + if (d == null) { + return null; + } else { + return new DigestImpl(digestor, algorithm, d); + } + } + + byte[] digest(ByteBuffer bb) { + // indirectly calls digestor.update(data, bb) + ACCESSOR.update(digestor, data, bb); + // indirectly calls digestor.digest(data) + return ACCESSOR.digest(digestor, data); + } +} +// END: day.end.bridges.DigestImpl \ No newline at end of file diff -r b704be5f8463 -r dbe255defdbb samples/messagedigest/src-new-api/org/apidesign/api/security/DigestImplementation.java --- a/samples/messagedigest/src-new-api/org/apidesign/api/security/DigestImplementation.java Sat Nov 15 13:12:46 2008 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - -package org.apidesign.api.security; - -import java.nio.ByteBuffer; -import org.apidesign.spi.security.Digestor; - -/** - * - * @author Jaroslav Tulach - */ -final class DigestImplementation { - private static final DigestorAccessorImpl ACCESSOR = new DigestorAccessorImpl(); - - private final Digestor digestor; - private final String algorithm; - private Data data; - - private DigestImplementation(Digestor digestor, String algorithm, Data d) { - this.digestor = digestor; - this.algorithm = algorithm; - this.data = d; - } - - static DigestImplementation create(Digestor digestor, String algorithm) { - Data d = ACCESSOR.create(digestor, algorithm); - if (d == null) { - return null; - } else { - return new DigestImplementation(digestor, algorithm, d); - } - } - - byte[] digest(ByteBuffer bb) { - ACCESSOR.update(digestor, data, bb); - return ACCESSOR.digest(digestor, data); - } -}