001package org.nasdanika.rag.core;
002
003import java.io.IOException;
004import java.util.ArrayList;
005import java.util.Collection;
006import java.util.List;
007import java.util.Map;
008import java.util.Map.Entry;
009import java.util.Objects;
010import java.util.zip.ZipFile;
011import java.util.zip.ZipInputStream;
012
013/**
014 * Stores entries in a list. Uses sum of abs element deltas as distance.
015 */
016public class ArrayListZipEntryStore extends ZipEntryStore<Integer> {
017
018        public ArrayListZipEntryStore() {
019                super();
020        }
021
022        public ArrayListZipEntryStore(ZipFile in) throws IOException {
023                super(in);
024        }
025
026        public ArrayListZipEntryStore(ZipInputStream in) throws IOException {
027                super(in);
028        }
029        
030        protected List<Entry<byte[], String>> entries;
031
032        @Override
033        protected void loadEntry(byte[] key, String value) throws IOException {
034                getEntries().add(Map.entry(key, value));                
035        }
036
037        @Override
038        public Collection<Entry<byte[], String>> getEntries() {
039                if (entries == null) {
040                         entries = new ArrayList<>();
041                }
042                return entries;
043        }
044
045        /**
046         * Longer vector is greater. For the same size - Manhattan distance.
047         */
048        @Override
049        protected Integer distance(byte[] a, byte[] b) {
050                int ld = a.length - b.length;
051                if (ld != 0) {
052                        return ld;
053                }
054
055                int result = 0;
056                for (int i = 0; i < a.length; ++i) {
057                        result += Math.abs(a[i] - b[i]);
058                }
059                return result;
060        }
061
062        @Override
063        protected int compareDistance(Integer a, Integer b) {
064                if (Objects.equals(a, b)) {
065                        return 0;
066                }
067                return a - b;
068        }
069
070}