From 82e6cce13eef79e7bcf8897e5265acaa6af935a6 Mon Sep 17 00:00:00 2001 From: Travis Redpath Date: Tue, 8 Mar 2022 21:28:59 -0600 Subject: [PATCH] Add value cache classes These were missed from earlier releases but have been added now. Creates caches of generic types. --- src/main/java/ca/hss/math/ValueCache.java | 93 ++++++++++++++++++++ src/main/java/ca/hss/math/ValueCache_MT.java | 40 +++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/main/java/ca/hss/math/ValueCache.java create mode 100644 src/main/java/ca/hss/math/ValueCache_MT.java diff --git a/src/main/java/ca/hss/math/ValueCache.java b/src/main/java/ca/hss/math/ValueCache.java new file mode 100644 index 0000000..83d1441 --- /dev/null +++ b/src/main/java/ca/hss/math/ValueCache.java @@ -0,0 +1,93 @@ +/** + * ValueCache.java + * + * Copyright 2022 Heartland Software Solutions Inc. + * + * Licensed 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 ca.hss.math; + +public class ValueCache { + int m_numEntries; + long m_call; + + static class Entry { + long m_call = -1; + Object m_key; + Object m_value; + }; + + Entry [] entries = null; + + public ValueCache(int numEntries) { + m_numEntries = numEntries; + entries = new Entry[m_numEntries]; + for (int i = 0; i < m_numEntries; i++) + entries[i] = new Entry(); + } + + public void clear() { + for (int i = 0; i < m_numEntries; i++) + entries[i].m_call = -1; + } + + public void put(T1 key, T2 value) { + if (m_numEntries == 0) + return; + m_call++; + if (m_call < 0) { + clear(); + m_call++; + } + + int i, oldest = 0; + for (i = 0; i < m_numEntries; i++) { + if (entries[i].m_call == -1) + break; + if (entries[i].m_call < entries[oldest].m_call) + oldest = i; + if (entries[i].m_key.equals(key)) { + entries[i].m_call = m_call; + return; + } + } + if (i == m_numEntries) + i = oldest; + + entries[i].m_call = m_call; + entries[i].m_key = key; + entries[i].m_value = value; + } + + @SuppressWarnings("unchecked") + public T2 get(T1 key) { + if (m_numEntries == 0) + return null; + + for (int i = 0; i < m_numEntries; i++) { + if (entries[i].m_call == -1) + return null; + if (entries[i].m_key.equals(key)) { + m_call++; + if (m_call < 0) { + clear(); + m_call++; + } + entries[i].m_call = m_call; + return (T2)entries[i].m_value; + } + } + return null; + } +} diff --git a/src/main/java/ca/hss/math/ValueCache_MT.java b/src/main/java/ca/hss/math/ValueCache_MT.java new file mode 100644 index 0000000..405f7b1 --- /dev/null +++ b/src/main/java/ca/hss/math/ValueCache_MT.java @@ -0,0 +1,40 @@ +/** + * ValueCache_MT.java + * + * Copyright 2022 Heartland Software Solutions Inc. + * + * Licensed 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 ca.hss.math; + +public class ValueCache_MT extends ValueCache { + public ValueCache_MT(int numEntries) { + super(numEntries); + } + + @Override + public synchronized void clear() { + super.clear(); + } + + @Override + public synchronized void put(T1 key, T2 value) { + super.put(key, value); + } + + @Override + public synchronized T2 get(T1 key) { + return super.get(key); + } +}