Oh, I didn't even think of that. Your approach makes more sense that way. On 24 June 2016 at 09:54, Gary Gregory <[email protected]> wrote:
> If we do that, then we have to change the return type of: > > lock() > lockInterruptibly() > > to void and create two new methods that return something since a try block > _must_ contain an assignment. > > What would you call these two methods? > > Gary > > On Fri, Jun 24, 2016 at 7:42 AM, Matt Sicker <[email protected]> wrote: > >> I kinda imagined AutoCloseableLock to implement both AutoCloseable and >> Lock. >> >> ---------- Forwarded message ---------- >> From: <[email protected]> >> Date: 24 June 2016 at 03:50 >> Subject: [1/4] logging-log4j2 git commit: Add AutoCloseableLock. >> To: [email protected] >> >> >> Repository: logging-log4j2 >> Updated Branches: >> refs/heads/AutoCloseableLock [created] 72d9978c6 >> >> >> Add AutoCloseableLock. >> >> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo >> Commit: >> http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/40efa80a >> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/40efa80a >> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/40efa80a >> >> Branch: refs/heads/AutoCloseableLock >> Commit: 40efa80a1a9745d7f9162b4f7ce96a7571a1c336 >> Parents: bc296c5 >> Author: Gary Gregory <[email protected]> >> Authored: Thu Jun 23 21:59:02 2016 -0700 >> Committer: Gary Gregory <[email protected]> >> Committed: Thu Jun 23 21:59:02 2016 -0700 >> >> ---------------------------------------------------------------------- >> .../logging/log4j/util/AutoCloseableLock.java | 44 ++++++++++++++++++++ >> 1 file changed, 44 insertions(+) >> ---------------------------------------------------------------------- >> >> >> >> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/40efa80a/log4j-api/src/main/java/org/apache/logging/log4j/util/AutoCloseableLock.java >> ---------------------------------------------------------------------- >> diff --git >> a/log4j-api/src/main/java/org/apache/logging/log4j/util/AutoCloseableLock.java >> b/log4j-api/src/main/java/org/apache/logging/log4j/util/AutoCloseableLock.java >> new file mode 100644 >> index 0000000..65aa581 >> --- /dev/null >> +++ >> b/log4j-api/src/main/java/org/apache/logging/log4j/util/AutoCloseableLock.java >> @@ -0,0 +1,44 @@ >> +/* >> + * 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.logging.log4j.util; >> + >> +import java.util.Objects; >> +import java.util.concurrent.locks.Lock; >> + >> +public class AutoCloseableLock implements AutoCloseable { >> + public static AutoCloseableLock lock(final Lock lock) { >> + Objects.requireNonNull(lock, "lock"); >> + lock.lock(); >> + return new AutoCloseableLock(lock); >> + } >> + >> + private final Lock lock; >> + >> + public AutoCloseableLock(final Lock lock) { >> + this.lock = lock; >> + } >> + >> + @Override >> + public void close() { >> + this.lock.unlock(); >> + } >> + >> + public AutoCloseableLock lock() { >> + this.lock.lock(); >> + return this; >> + } >> +} >> \ No newline at end of file >> >> >> >> >> -- >> Matt Sicker <[email protected]> >> > > > > -- > E-Mail: [email protected] | [email protected] > Java Persistence with Hibernate, Second Edition > <http://www.manning.com/bauer3/> > JUnit in Action, Second Edition <http://www.manning.com/tahchiev/> > Spring Batch in Action <http://www.manning.com/templier/> > Blog: http://garygregory.wordpress.com > Home: http://garygregory.com/ > Tweet! http://twitter.com/GaryGregory > -- Matt Sicker <[email protected]>
