diff -r 000000000000 -r 716af5f2ebd1 samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java Sat Jun 14 09:52:23 2008 +0200 @@ -0,0 +1,44 @@ + +package org.apidesign.api.security; + +import java.nio.ByteBuffer; +import org.apidesign.impl.security.friendapi.DigestImplementation; +import org.apidesign.impl.security.friendapi.DigestProvider; +import java.util.ServiceLoader; + +/** MessageDigest extends MessageDigestSpi, that means the javadoc + * + * @author Jaroslav Tulach + */ +// BEGIN: day.end.bridges.Digest +public final class Digest { + private final DigestImplementation impl; + + /** Factory method is better than constructor */ + private Digest(DigestImplementation impl) { + this.impl = impl; + } + + /** Factory method to create digest for an algorithm. + */ + public static Digest getInstance(String algorithm) { + for (DigestProvider dp : ServiceLoader.load(DigestProvider.class)) { + DigestImplementation impl = dp.create(algorithm); + if (impl != null) { + return new Digest(impl); + } + } + throw new IllegalArgumentException(algorithm); + } + + // + // these methods are kept the same as in original MessageDigest, + // but for simplicity choose just some from the original API + // + + public byte[] digest(ByteBuffer bb) { + impl.update(bb); + return impl.digest(); + } +} +// END: day.end.bridges.Digest