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 @ 44897

History | View | Annotate | Download (4.59 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.Coercion;
37
import org.gvsig.tools.dataTypes.CoercionException;
38
import org.gvsig.tools.dataTypes.DataType;
39
import org.gvsig.tools.dataTypes.DataTypeUtils;
40
import org.gvsig.tools.dataTypes.DataTypesManager;
41

    
42
public class MemoryBasicTypesIndexProvider<T> extends AbstractFeatureIndexProvider {
43

    
44
    private TreeMap<T,ListOfLong> index = null;
45
    private final Class<T> classt;
46
    private final Coercion coercion;
47

    
48
    public MemoryBasicTypesIndexProvider(Class<T> classt) {
49
        this.classt = classt;
50
        this.coercion = ToolsLocator.getDataTypesManager().getDataType(classt).getCoercion();
51
    }
52

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

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

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

    
91
    @Override
92
    public List match(Object key_o) throws FeatureIndexException {
93
        T key_s;
94
        try {
95
            key_s = (T) this.coercion.coerce(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
}