001package de.monochromata.anaphors.cog.activation; 002 003import static java.lang.Long.compare; 004 005import java.util.WeakHashMap; 006import java.util.concurrent.atomic.AtomicLong; 007 008/** 009 * A simplistic memory-less activation that decreases activation for existing 010 * elements whenever a new element is created and never increments activation. 011 */ 012@Deprecated 013public class LIFOFormula implements ActivationFormula { 014 015 private final AtomicLong counter = new AtomicLong(); 016 private final WeakHashMap<Activatable, Long> positions = new WeakHashMap<>(); 017 018 @Override 019 public LIFOActivation estimateActivation(final Activatable activatable, final long timestamp) { 020 final Long existingPosition = positions.get(activatable); 021 if (existingPosition == null) { 022 return createAndSaveNewPosition(activatable); 023 } 024 return new LIFOActivation(existingPosition); 025 } 026 027 protected LIFOActivation createAndSaveNewPosition(final Activatable activatable) { 028 final long newPosition = counter.getAndIncrement(); 029 positions.put(activatable, newPosition); 030 return new LIFOActivation(newPosition); 031 } 032 033 public static class LIFOActivation implements EstimatedActivationValue { 034 035 private final long position; 036 037 /** 038 * Used in contract testing. 039 */ 040 @SuppressWarnings("unused") 041 protected LIFOActivation() { 042 this(0l); 043 } 044 045 public LIFOActivation(final long position) { 046 this.position = position; 047 } 048 049 @Override 050 public int compareTo(final EstimatedActivationValue other) { 051 if (other.getClass() != getClass()) { 052 throw new IllegalArgumentException("Invalid argument type: " + other.getClass()); 053 } 054 return -compare(position, ((LIFOActivation) other).position); 055 } 056 057 public long getPosition() { 058 return position; 059 } 060 061 @Override 062 public int hashCode() { 063 final int prime = 31; 064 int result = 1; 065 result = prime * result + (int) (position ^ (position >>> 32)); 066 return result; 067 } 068 069 @Override 070 public boolean equals(final Object obj) { 071 if (this == obj) { 072 return true; 073 } 074 if (obj == null) { 075 return false; 076 } 077 if (getClass() != obj.getClass()) { 078 return false; 079 } 080 final LIFOActivation other = (LIFOActivation) obj; 081 if (position != other.position) { 082 return false; 083 } 084 return true; 085 } 086 087 } 088 089}