|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2020 Alen Turkovic |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.github.alturkovic.lock; |
| 26 | + |
| 27 | +import java.util.List; |
| 28 | +import java.util.function.Supplier; |
| 29 | +import lombok.Data; |
| 30 | +import org.springframework.util.Assert; |
| 31 | +import org.springframework.util.StringUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * An abstract lock used as a base for all locks that operate with only 1 key instead of multiple keys. |
| 35 | + */ |
| 36 | +@Data |
| 37 | +public abstract class AbstractSimpleLock implements Lock { |
| 38 | + private final Supplier<String> tokenSupplier; |
| 39 | + |
| 40 | + @Override |
| 41 | + public String acquire(final List<String> keys, final String storeId, final long expiration) { |
| 42 | + Assert.isTrue(keys.size() == 1, "Cannot acquire lock for multiple keys with this lock"); |
| 43 | + |
| 44 | + final var token = tokenSupplier.get(); |
| 45 | + if (StringUtils.isEmpty(token)) { |
| 46 | + throw new IllegalStateException("Cannot lock with empty token"); |
| 47 | + } |
| 48 | + |
| 49 | + return acquire(keys.get(0), storeId, token, expiration); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean release(final List<String> keys, final String storeId, final String token) { |
| 54 | + Assert.isTrue(keys.size() == 1, "Cannot release lock for multiple keys with this lock"); |
| 55 | + return release(keys.get(0), storeId, token); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public boolean refresh(final List<String> keys, final String storeId, final String token, final long expiration) { |
| 60 | + Assert.isTrue(keys.size() == 1, "Cannot refresh lock for multiple keys with this lock"); |
| 61 | + return refresh(keys.get(0), storeId, token, expiration); |
| 62 | + } |
| 63 | + |
| 64 | + protected abstract String acquire(String key, String storeId, String token, long expiration); |
| 65 | + protected abstract boolean release(String key, String storeId, String token); |
| 66 | + protected abstract boolean refresh(String key, String storeId, String token, long expiration); |
| 67 | +} |
0 commit comments