MentaBean

Rev

Rev 162 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 soliveira 1
/*
22 soliveira 2
 * This program is free software: you can redistribute it and/or modify
3
 * it under the terms of the GNU General Public License as published by
4
 * the Free Software Foundation, either version 3 of the License, or
5
 * (at your option) any later version.
6
 *
7
 * This program is distributed in the hope that it will be useful,
2 soliveira 8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 soliveira 9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
 *
15
 * MentaBean => http://www.mentabean.org
16
 * Author: Sergio Oliveira Jr. (sergio.oliveira.jr@gmail.com)
2 soliveira 17
 */
18
package org.mentabean.type;
19
 
20
import java.sql.PreparedStatement;
21
import java.sql.ResultSet;
22
import java.sql.SQLException;
23
 
24
import org.mentabean.DBType;
25
 
61 soliveira 26
public class EnumValueType implements DBType<Enum<?>>, SizedType {
2 soliveira 27
 
28
        private final Class<? extends Enum<?>> enumType;
61 soliveira 29
        private final int size;
2 soliveira 30
 
61 soliveira 31
        private boolean canBeNull = false;
32
 
33
        @Override
34
        public boolean canBeNull() {
35
                return canBeNull;
36
        }
37
 
38
        public EnumValueType nullable(boolean flag) {
39
                EnumValueType d = new EnumValueType(enumType);
40
                d.canBeNull = flag;
41
                return d;
42
        }
43
 
20 soliveira 44
        public EnumValueType(final Class<? extends Enum<?>> enumType) {
2 soliveira 45
 
46
                this.enumType = enumType;
61 soliveira 47
                this.size = calcSize();
2 soliveira 48
        }
49
 
50
        @Override
61 soliveira 51
        public String getAnsiType() {
52
                return "varchar";
53
        }
54
 
55
        public EnumValueType size(int size) {
56
                throw new UnsupportedOperationException("Cannot set the size on a " + this);
57
        }
58
 
59
        @Override
60
        public int getSize() {
61
                return size;
62
        }
63
 
64
        private int calcSize() {
162 erico 65
                Enum<?>[] all = enumType.getEnumConstants();
61 soliveira 66
                int max = 0;
162 erico 67
                for (Enum<?> o : all) {
68
                        max = Math.max(max, o.name().length());
61 soliveira 69
                }
70
                return max;
71
        }
72
 
73
        @Override
2 soliveira 74
        public String toString() {
75
 
76
                return this.getClass().getSimpleName() + ": " + enumType.getSimpleName();
77
        }
61 soliveira 78
 
2 soliveira 79
        @Override
10 soliveira 80
        public Enum<?> getFromResultSet(final ResultSet rset, final int index) throws SQLException {
2 soliveira 81
 
82
                final String s = rset.getString(index);
83
 
84
                if (s == null) {
85
                        return null;
86
                }
87
 
88
                return Enum.valueOf((Class) enumType, s);
89
 
90
        }
91
 
92
        @Override
10 soliveira 93
        public Enum<?> getFromResultSet(final ResultSet rset, final String field) throws SQLException {
2 soliveira 94
 
95
                final String s = rset.getString(field);
96
 
97
                if (s == null) {
98
                        return null;
99
                }
100
 
101
                return Enum.valueOf((Class) enumType, s);
102
 
103
        }
104
 
105
        @Override
106
        public Class<? extends Object> getTypeClass() {
107
 
108
                return enumType;
109
        }
110
 
111
        @Override
10 soliveira 112
        public void bindToStmt(final PreparedStatement stmt, final int index, final Enum<?> value) throws SQLException {
2 soliveira 113
 
114
                if (value == null) {
115
 
116
                        stmt.setString(index, null);
117
 
187 erico 118
                        //Support for overwritten enums (superclass on left)
119
                } else if (enumType.isAssignableFrom(value.getClass())) {
2 soliveira 120
 
162 erico 121
                        final String s = value.name();
2 soliveira 122
 
123
                        stmt.setString(index, s);
124
 
125
                } else {
126
 
187 erico 127
                        throw new IllegalArgumentException("Value '"+value+"' from '"+value.getClass()+"' is not an enum!");
2 soliveira 128
                }
129
 
130
        }
131
}