[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0

Type safeing my class.

Asked by PaulCaswell in Java Programming Language

Tags: Java, EnumSet, Generics

Hi All,

I've put together a class that helps me manipulate database tables quite succinctly and it works quite well. See the <unsafe> code below ...

Note that I have cut down the code for posting purposes to the point where it won't actually work but I hope it demonstrates my problem.

I am now trying to make it type safe and I'm having difficulties typeing the EnumSets. See SafeChangeTable and SafeTable.

In particular I am trying to remove the use of EnumSet. My first attempt was the obvious:

  public String updateQuery ( EnumSet<Column> key, Column[] columns ) {

but this gives me the error:

type parameter Column is not within its bound

A bit of research found one method:

  public String updateQuery (EnumSet<? extends Enum<Column>> key, Column[] columns ) {

which removes the error but then when I try:

  public String updateQuery (EnumSet<? extends Enum<Column>> key) {
    // If columns not supplied, assume all columns.
    return ( updateQuery(key, columns) );
  }

I get the error:

cannot find symbol; symbol  : method updateQuery(java.util.EnumSet<? extends java.lang.Enum<Column>>,Column[]), location: class SafeTable<Column>

Is EnumSet<? extends Enum<Column>> the right way of doing this? If so, how do I avoid the second error?

Thanks in advance.

Paul
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
// .... <Unsafe version using Enum> ...
 
public class ChangeTable extends Table {
  static final Map<Enum,Field.Type> columnTypes = new HashMap<Enum,Field.Type>();
  
  enum Column {
      Change(Field.Type.String),
      Date(Field.Type.Long);
 
      // Keep track of the column types.
      Column(Field.Type type) {
        columnTypes.put(this, type);
      }
  };
  
  public ChangeTable () {
    super("ChangeTable", Column.values(), columnTypes);
  }
}
 
public class Table {
  final String tableName;
  final Enum[] columns;
  final Map<Enum,Field.Type> types;
  Map<Enum,Object> values = new HashMap<Enum,Object>();
  
  protected Table (String tableName, Enum [] columns, Map<Enum,Field.Type> types) {
    this.tableName = tableName;
    this.columns = columns;
    this.types = types;
  }
 
  // Returns a query that will update all records matching the key.
  private String updateQuery ( EnumSet key, Enum[] columns ) {
    String query = "UPDATE "+tableName+" SET ";
    // SET - All update columns not in key.
    for ( int i = 0; i < columns.length; i++ ) {
      Enum col = columns[i];
      // Not key column.
      if ( !key.contains(col) ) {
        // Add field to query.
        query += col.name() + " = " + values.get(col).toString();
      }
    }
    // WHERE All columns in key.
    query += " WHERE " + keyValues(key);
    return query;
  }
 
  public String updateQuery (EnumSet key) {
    // If columns not supplied, assume all columns.
    return ( updateQuery(key, columns) );
  }
 
  public String updateQuery (EnumSet key, EnumSet updateColumns) {
    // Allow an EnumSet of Columns to update.
    return updateQuery(key, (Enum[]) updateColumns.toArray() );
  }
 
  private String keyValues ( EnumSet key ) {
    String keyValues = "";
    for ( Iterator i = key.iterator(); i.hasNext(); ) {
      // Pull out the column.
      Enum col = (Enum)i.next();
      String val = values.get( col ).toString();
      keyValues += col.name() + " = " + val;
    }
    return keyValues;
  }
 
}
 
public class Field {
  
  enum Type {
    String,
    Long;
  }
  
  public Field () {
  }
}
 
// .... <Attempt at safe version, not using Enum but still using EnumSet> ... 
 
public class SafeChangeTable extends SafeTable<SafeChangeTable.Column> {
  static final Map<Column,Field.Type> columnTypes = new HashMap<Column,Field.Type>();
 
  enum Column {
      Change(Field.Type.String),
      Date(Field.Type.Long);
 
      // Keep track of the column types.
      Column(Field.Type type) {
        columnTypes.put(this, type);
      }
  };
 
  public SafeChangeTable () {
    super("ChangeTable", Column.values(), columnTypes);
  }
}
 
public class SafeTable<Column extends Enum> {
  final String tableName;
  final Column[] columns;
  final Map<Column,Field.Type> types;
  Map<Column,Object> values = new HashMap<Column,Object>();
 
  protected SafeTable (String tableName, Column [] columns, Map<Column,Field.Type> types) {
    this.tableName = tableName;
    this.columns = columns;
    this.types = types;
  }
 
  public String updateQuery ( EnumSet key, Column[] columns ) {
    String query = "UPDATE "+tableName+" SET ";
    // SET - All update columns not in key.
    for ( int i = 0; i < columns.length; i++ ) {
      Column col = columns[i];
      // Not key column.
      if ( !key.contains(col) ) {
        // Add field to query.
        query += col.name() + " = " + values.get(col);
      }
    }
    // WHERE All columns in key.
    query += " WHERE " + keyValues(key);
    return query;
  }
 
  public String updateQuery (EnumSet key) {
    // If columns not supplied, assume all columns.
    return ( updateQuery(key, columns) );
  }
 
  public String updateQuery (EnumSet key, EnumSet updateColumns) {
    // Allow an EnumSet of Columns to update.
    return updateQuery(key, (Column[]) updateColumns.toArray() );
  }
 
  private String keyValues ( EnumSet key ) {
    String keyValues = "";
    for ( Iterator i = key.iterator(); i.hasNext(); ) {
      // Pull out the column.
      Enum col = (Enum)i.next();
      String val = values.get( col ).toString();
      keyValues += col.name() + " = " + val;
    }
    return keyValues;
  }
 
}
[+][-]07/18/09 03:41 PM, ID: 24887591Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Java Programming Language
Tags: Java, EnumSet, Generics
Sign Up Now!
Solution Provided By: CEHJ
Participating Experts: 1
Solution Grade: A
 
[+][-]07/18/09 09:18 AM, ID: 24886499Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/18/09 10:40 AM, ID: 24886697Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/09 10:49 AM, ID: 24886715Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/18/09 10:55 AM, ID: 24886732Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/09 11:10 AM, ID: 24886781Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/18/09 11:16 AM, ID: 24886801Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/18/09 11:38 AM, ID: 24886854Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/09 11:43 AM, ID: 24886868Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/09 11:59 AM, ID: 24886906Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/18/09 12:02 PM, ID: 24886918Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/18/09 12:10 PM, ID: 24886940Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/09 12:34 PM, ID: 24887018Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/09 12:45 PM, ID: 24887045Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/18/09 12:46 PM, ID: 24887049Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/18/09 01:19 PM, ID: 24887145Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/09 01:26 PM, ID: 24887177Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/09 01:49 PM, ID: 24887295Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/18/09 03:17 PM, ID: 24887530Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/09 03:31 PM, ID: 24887560Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/09 03:44 PM, ID: 24887604Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/09 03:46 PM, ID: 24887612Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/19/09 12:56 AM, ID: 24888598Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_3_20090701_SELECT_ZONES