NO-JIRA: Add ExtendableAccessor A more conventient and more typesafe variation on RecordAccessor.
Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/7d028ae5 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/7d028ae5 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/7d028ae5 Branch: refs/heads/proton-go Commit: 7d028ae58ca167a25aa3ac25529a12292631a025 Parents: 05beb21 Author: Bozo Dragojevic <[email protected]> Authored: Thu Sep 17 13:05:21 2015 +0200 Committer: Bozo Dragojevic <[email protected]> Committed: Thu Sep 17 13:06:19 2015 +0200 ---------------------------------------------------------------------- .../qpid/proton/engine/ExtendableAccessor.java | 57 ++++++++++++++++++++ .../proton/engine/EventExtensibilityTest.java | 17 ++---- 2 files changed, 60 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/7d028ae5/proton-j/src/main/java/org/apache/qpid/proton/engine/ExtendableAccessor.java ---------------------------------------------------------------------- diff --git a/proton-j/src/main/java/org/apache/qpid/proton/engine/ExtendableAccessor.java b/proton-j/src/main/java/org/apache/qpid/proton/engine/ExtendableAccessor.java new file mode 100644 index 0000000..c30b48e --- /dev/null +++ b/proton-j/src/main/java/org/apache/qpid/proton/engine/ExtendableAccessor.java @@ -0,0 +1,57 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.proton.engine; + +/** + * A typesafe convenience class for associating additional data with {@link Extendable} classes. + * <p> + * An instance of <code>ExtendableAccessor</code> uses itself as the key in the {@link Extendable#attachments()} + * so it's best instantiated as a static final member. + * <pre><code> + * class Foo extends BaseHandler { + * private static ExtendableAccessor<Link, Bar> LINK_BAR = new ExtendableAccessor<>(Bar.class); + * void onLinkRemoteOpen(Event e) { + * Bar bar = LINK_BAR.get(e.getLink()); + * if (bar == null) { + * bar = new Bar(); + * LINK_BAR.set(e.getLink(), bar); + * } + * } + * } + * </code></pre> + * + * @param <E> An {@link Extendable} type where the data is to be stored + * @param <T> The type of the data to be stored + */ +public final class ExtendableAccessor<E extends Extendable, T> { + private final Class<T> klass; + public ExtendableAccessor(Class<T> klass) { + this.klass = klass; + } + + public T get(E e) { + return e.attachments().get(this, klass); + } + + public void set(E e, T value) { + e.attachments().set(this, klass, value); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/7d028ae5/proton-j/src/test/java/org/apache/qpid/proton/engine/EventExtensibilityTest.java ---------------------------------------------------------------------- diff --git a/proton-j/src/test/java/org/apache/qpid/proton/engine/EventExtensibilityTest.java b/proton-j/src/test/java/org/apache/qpid/proton/engine/EventExtensibilityTest.java index 64c7a17..53a247b 100644 --- a/proton-j/src/test/java/org/apache/qpid/proton/engine/EventExtensibilityTest.java +++ b/proton-j/src/test/java/org/apache/qpid/proton/engine/EventExtensibilityTest.java @@ -155,19 +155,8 @@ public class EventExtensibilityTest extends TestCase { /** * making this accessor public allows for easy binding to the scripting language */ - public static final RecordAccessor<ExtraInfo> extraInfoAccessor = new RecordAccessor<ExtraInfo>() { + public static final ExtendableAccessor<Event, ExtraInfo> extraInfoAccessor = new ExtendableAccessor<>(ExtraInfo.class); - @Override - public ExtraInfo get(Record r) { - return r.get(this, ExtraInfo.class); - } - - @Override - public void set(Record r, ExtraInfo value) { - r.set(this, ExtraInfo.class, value); - } - - }; private Event impl; public ExtraEventImpl(Event impl) { @@ -185,7 +174,7 @@ public class EventExtensibilityTest extends TestCase { @Override public ExtraInfo getExtraInfo() { - return extraInfoAccessor.get(impl.attachments()); + return extraInfoAccessor.get(impl); } // ---- delegate methods for the Event @@ -270,7 +259,7 @@ public class EventExtensibilityTest extends TestCase { public void onReactorInit(Event e) { ExtraInfo extra = new ExtraInfo(); extra.foo = 1234; - ExtraEventImpl.extraInfoAccessor.set(e.attachments(), extra); + ExtraEventImpl.extraInfoAccessor.set(e, extra); e.redispatch(ExtraEvent.ExtraTypes.FOO, this); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
