Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / indexes / memorybasictypes / MemoryBasicTypesIndexProvider.java @ 44895

History | View | Annotate | Download (4.62 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.impl.indexes.memorybasictypes;
25

    
26
import java.util.List;
27
import java.util.TreeMap;
28
import java.util.logging.Level;
29
import java.util.logging.Logger;
30

    
31
import org.gvsig.fmap.dal.exception.DataException;
32
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
33
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
34
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider;
35
import org.gvsig.tools.ToolsLocator;
36
import org.gvsig.tools.dataTypes.CoercionException;
37
import org.gvsig.tools.dataTypes.DataType;
38
import org.gvsig.tools.dataTypes.DataTypeUtils;
39
import org.gvsig.tools.dataTypes.DataTypesManager;
40

    
41
public class MemoryBasicTypesIndexProvider<T> extends AbstractFeatureIndexProvider {
42

    
43
    private TreeMap<T,ListOfLong> index = null;
44
    private final Class<T> classt;
45
    private final int dataType;
46

    
47
    public MemoryBasicTypesIndexProvider(Class<T> classt) {
48
        this.classt = classt;
49
        this.dataType = ToolsLocator.getDataTypesManager().getDataType(classt).getType(); // getCoercion()
50
    }
51

    
52
    @Override
53
    public void initialize() {
54
        try {
55
            this.index = new TreeMap<>();
56
        } catch (Exception e) {
57
            throw new RuntimeException();
58
        }
59
    }
60

    
61
    @Override
62
    public void delete(Object key_o, FeatureReferenceProviderServices fref) {
63
        T key_s = (T) key_o;
64
        long oid = (Long) fref.getOID();
65
        ListOfLong items = this.index.get(key_s);
66
        if( items == null ) {
67
            return;
68
        }
69
        items.remove(oid);
70
        if( items.isEmpty() ) {
71
            this.index.remove(key_s);
72
        }
73
    }
74

    
75
    @Override
76
    public void insert(Object key_o, FeatureReferenceProviderServices fref) {
77
        if (key_o == null) {
78
            return;
79
        }
80
        T key_s = (T) key_o;
81
        long oid = (Long) fref.getOID();
82
        ListOfLong items = this.index.get(key_s);
83
        if( items == null ) {
84
            items = new ArrayListOfLong();
85
            this.index.put(key_s, items);
86
        }
87
        items.add(oid);
88
    }
89

    
90
    @Override
91
    public List match(Object key_o) throws FeatureIndexException {
92
//        T key_s = this.classt.cast(key_o);
93
        T key_s;
94
        try {
95
            key_s = (T) DataTypeUtils.coerce(this.dataType, key_o);
96
        } catch (CoercionException ex) {
97
            return null;
98
        }
99
        if( !this.index.containsKey(key_s) ) {
100
            return null;
101
        }
102
        ListOfLong items = this.index.get(key_s);
103
        if( items == null ) {
104
            return null;
105
        }
106
        return items;
107
    }
108

    
109
    public List match(Object min, Object max) {
110
        throw new UnsupportedOperationException(
111
                "Can't perform this kind of search.");
112
    }
113

    
114
    @Override
115
    public List nearest(int count, Object value) throws FeatureIndexException {
116
        throw new UnsupportedOperationException(
117
                "Can't perform this kind of search.");
118
    }
119

    
120
    @Override
121
    public boolean isMatchSupported() {
122
        return true;
123
    }
124

    
125
    @Override
126
    public boolean isNearestSupported() {
127
        return false;
128
    }
129

    
130
    @Override
131
    public boolean isNearestToleranceSupported() {
132
        return false;
133
    }
134

    
135
    @Override
136
    public boolean isRangeSupported() {
137
        return false;
138
    }
139

    
140
    @Override
141
    public List nearest(int count, Object value, Object tolerance)
142
            throws FeatureIndexException {
143
        throw new UnsupportedOperationException();
144
    }
145

    
146
    @Override
147
    public List range(Object value1, Object value2)
148
            throws FeatureIndexException {
149
        throw new UnsupportedOperationException();
150
    }
151

    
152
    @Override
153
    public void clear() throws DataException {
154
        this.index.clear();
155
    }
156
}