lazy
Project: http://git-wip-us.apache.org/repos/asf/bval/repo Commit: http://git-wip-us.apache.org/repos/asf/bval/commit/29a6ded2 Tree: http://git-wip-us.apache.org/repos/asf/bval/tree/29a6ded2 Diff: http://git-wip-us.apache.org/repos/asf/bval/diff/29a6ded2 Branch: refs/heads/bv2 Commit: 29a6ded2f44f68fc0b9dda8f213d354990e6457f Parents: bc99828 Author: Matt Benson <[email protected]> Authored: Wed Nov 15 14:50:30 2017 -0600 Committer: Matt Benson <[email protected]> Committed: Wed Nov 15 16:53:42 2017 -0600 ---------------------------------------------------------------------- .../main/java/org/apache/bval/util/Lazy.java | 51 ++++++++++++++++++++ .../main/java/org/apache/bval/util/LazyInt.java | 49 +++++++++++++++++++ 2 files changed, 100 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/bval/blob/29a6ded2/bval-core/src/main/java/org/apache/bval/util/Lazy.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/Lazy.java b/bval-core/src/main/java/org/apache/bval/util/Lazy.java new file mode 100644 index 0000000..b1cd9c3 --- /dev/null +++ b/bval-core/src/main/java/org/apache/bval/util/Lazy.java @@ -0,0 +1,51 @@ +/* + * 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.bval.util; + +import java.util.Optional; +import java.util.function.Supplier; + +/** + * @since 2.0 + * + * @param <T> + */ +public class Lazy<T> implements Supplier<T> { + private T value; + private Supplier<T> init; + + public Lazy(Supplier<T> init) { + this.init = Validate.notNull(init); + } + + @Override + public T get() { + if (init != null) { + synchronized (this) { + if (init != null) { + value = init.get(); + init = null; + } + } + } + return value; + } + + public Optional<T> optional() { + return Optional.ofNullable(value); + } +} http://git-wip-us.apache.org/repos/asf/bval/blob/29a6ded2/bval-core/src/main/java/org/apache/bval/util/LazyInt.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/LazyInt.java b/bval-core/src/main/java/org/apache/bval/util/LazyInt.java new file mode 100644 index 0000000..44e09dd --- /dev/null +++ b/bval-core/src/main/java/org/apache/bval/util/LazyInt.java @@ -0,0 +1,49 @@ +/* + * 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.bval.util; + +import java.util.OptionalInt; +import java.util.function.IntSupplier; + +/** + * @since 2.0 + */ +public class LazyInt implements IntSupplier { + private int value; + private IntSupplier init; + + public LazyInt(IntSupplier init) { + this.init = Validate.notNull(init); + } + + @Override + public int getAsInt() { + if (init != null) { + synchronized (this) { + if (init != null) { + value = init.getAsInt(); + init = null; + } + } + } + return value; + } + + public synchronized OptionalInt optional() { + return init == null ? OptionalInt.of(value) : OptionalInt.empty(); + } +}
