|
-
Mar 7th, 2005, 09:31 PM
#1
Thread Starter
Fanatic Member
Class Design
Need help with class design. I've got a DataType Class and I need help on making a Collection of this class. Kinda new to Java to please understand. Thanks.
This is DataType class
VB Code:
import java.util.*;
public class DataType{
String sqltype,
javatype,
csharptype,
vbtype;
public DataType(){
}
public DataType(String sqltype,
String javatype,
String csharptype,
String vbtype){
this.sqltype=sqltype;
this.javatype=javatype;
this.csharptype=csharptype;
this.vbtype=vbtype;
}
. . .
}
Now I want to make a collection of that class. Something like
VB Code:
import java.util.*;
public class DataTypes extends [i]IDontKnow[/i]{
public DataTypes(){}
}
where I can return something of type DataType and a public function add for adding of type DataType. Please help. Any recommendations and links for tutorials are greatly appreciated. Thank you.
Last edited by nebulom; Mar 9th, 2005 at 09:27 PM.
-
Mar 7th, 2005, 11:24 PM
#2
Dazed Member
Re: Class Design
Im not too good with design issues but i will try and help. You would probably have to implement the Collection interface and provide implementation for all of the methods that are contained within or extend one of the concrete classes such as TreeSet or ArrayList. As for which one to do i couldn't give you a suggestion. I just use the concrete classes for most of my programming.
-
Mar 8th, 2005, 03:11 AM
#3
Thread Starter
Fanatic Member
Re: Class Design
Thanks Dilenger but can you give some codes. I don't know much on Java. Or perhaps... a link or something. Thank you very much.
Btw, why did you give up being a mod? You could have been with NoteMe. You two can be a mod.
-
Mar 8th, 2005, 10:30 AM
#4
Addicted Member
Re: Class Design
so do you want a collection to take data types? or make the datatype class a collection?
Which ever it is, just make that class implement the Collection interface, and implement the methods, its hard to say exactly how to do this without understanding what you want to achieve, the methods on Collection are pretty well spec'd int he Java doc....
Do you actually want to make this class a Collection? or simply have the functionality of the collection class, and therefore make all the methods return DataType rather than Object.....
Andy
-
Mar 8th, 2005, 11:04 AM
#5
Dazed Member
Re: Class Design
Yeah i was confused about what he wanted to do also.
Posted by nebulom
Btw, why did you give up being a mod? You could have been with NoteMe. You two can be a mod.
I originally decided to quit programming and try somthing else but i since decided to just do it for fun and not place any unrealistic expectations on it. Plus i am moving from new jersey to tennessee in three weeks so with all of the stuf i have to get in order before the move and then after my time would be limited.
-
Mar 8th, 2005, 09:13 PM
#6
Thread Starter
Fanatic Member
Re: Class Design
Thanks guys.
I just wanna make the DataTypes a collection of DataType class. Something like this on C#
VB Code:
using System;
namespace dtgen_0_0_3
{
/// <summary>
/// Summary description for DataTypes.
/// </summary>
public class DataTypes:System.Collections.CollectionBase
{
public DataTypes()
{
//
// TODO: Add constructor logic here
//
this.Add(new DataType("bigint","Int64","Integer"));
this.Add(new DataType("binary","byte[]","Byte()"));
this.Add(new DataType("bit","bool","Boolean"));
this.Add(new DataType("char","string","String"));
this.Add(new DataType("datetime","DateTime","DateTime"));
this.Add(new DataType("decimal","decimal","Decimal"));
this.Add(new DataType("float","float","Double"));
this.Add(new DataType("image","byte[]","Byte()"));
this.Add(new DataType("int","int","Integer"));
this.Add(new DataType("money","decimal","Decimal"));
this.Add(new DataType("nchar","string","String"));
this.Add(new DataType("ntext","string","String"));
this.Add(new DataType("numeric","decimal","Decimal"));
this.Add(new DataType("nvarchar","string","String"));
this.Add(new DataType("real","Single","Single"));
this.Add(new DataType("smalldatetime","DateTime","DateTime"));
this.Add(new DataType("smallint","short","Short"));
this.Add(new DataType("smallmoney","decimal","Decimal"));
this.Add(new DataType("sql_variant","object","Object"));
this.Add(new DataType("text","string","String"));
this.Add(new DataType("timestamp","DateTime","DateTime"));
this.Add(new DataType("tinyint","byte","Byte"));
this.Add(new DataType("uniqueidentifier","Guid","Guid"));
this.Add(new DataType("varbinary","byte[]","Byte()"));
this.Add(new DataType("varchar","string","String"));
}
public DataType this[int i]
{
get{ return (DataType)this.InnerList[i];}
}
public void Add(DataType v)
{
this.InnerList.Add(v);
}
}
}
How do I convert this to Java? Thanks.
-
Mar 9th, 2005, 06:50 AM
#7
Addicted Member
Re: Class Design
Ok so you want a collection of data types, this is pretty easy to do, you can either write a class and implement the COollection interface, or you can hi jack the method you require from teh collection interface abd make them retrun something sensible instaed of an object.
Personally if you don't need the full compatibility with the collection interface i'd just implement all the collection methods and change the return types from Object to Data type, that way you won't have to do any visible vasting, i.e.
Code:
DataType d = (DataType)collection.get(i);
I think .Net has the luxury of generics (correct me if im wrong) which means you can create a collection of a certina type, therefore all return values will be of that type, unfortunatley Java 1.4 which is what i'm using dosen't have that, i'd have to upgrade to 1.5 whci i'm not allowed to do until the product development manager deicdes we can release with it!
Ok below is a kind of implementation of a collection class.
Code:
public class DataTypeCollection{
private ArrayList list = new ArrayList();
public void add(DataType type){
this.list.add(type);
}
public void remove(DataType type){
this.list.remove(type);
}
public DataType get(int index){
return (DataType)this.list.get(index);
}
public int size(){
return this.list.size();
}
public boolean contains(DataType dataType){
return this.ist.contains(dataType);
}
}
now that isn't a full implementation but you see what i'm doing, onstead of returning Objkects which is what any class implementing Collections will do i'm returning a DataType and encapsulating all my casting.....
This would achieve what you wanted to do, if you wanted full Collections compatibility you could write another Wrapper class around this which would implement teh Collection interface properly.... if you don't need this then the class i've semi written above will remove the headache of casting etc...
does that make sense? let me know i've been vague or missed the point and i'll try again...
Andy
-
Mar 9th, 2005, 07:06 AM
#8
Thread Starter
Fanatic Member
Re: Class Design
Here I've got
VB Code:
import java.util.*;
public class DataTypes{
ArrayList list=new ArrayList();
public DataTypes(){
this.list.add(new DataType("int","int","int","Integer"));
this.list.add(new DataType("bigint","int","Int64","Integer"));
. . .
}
public void remove(DataType v){
this.list.remove(v);
}
public DataType get(int i){
return (DataType)this.list.get(i);
}
public int size(){
return this.list.size();
}
public boolean contains(DataType v){
return this.list.contains(v);
}
}
Thanks a lot. But Andy, it says,
VB Code:
Note: DataTypes.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
I'm sorry to ask you what's this thing. Furthermore, I want to access the class like this
VB Code:
foreach(DataType in DataTypes) . . .
Can I do this?
Again, thanks a lot. And sorry of the bother.
-
Mar 9th, 2005, 07:17 AM
#9
Addicted Member
Re: Class Design
ah hah! your using Java 5, or 1.5 or whatever they decided to call it aren't you?
ok, its should be straightforward then as you can use generics...
Code:
ArrayList<DataType> list = new ArrayList<DataType>();
list.add(new DataType(args....));
DataType type = list.get(0);
and that should be it, you can then use the Java 1.5 things to loop around
the collection i.e.
Code:
for(DataType d: list) {
//Now you can use d, and it will be the current element in the list!
}
that what you needed?
-
Mar 9th, 2005, 07:21 AM
#10
Addicted Member
Re: Class Design
the unchecked error you get is because i wrote the first snippet thinking you were on 1.4, now when we cast the object rfomthe array list into a DataType we don't check to see if the object is in fact a data type.... in theory we should actually wrap a try catch round it and deal with the problem if its now a datatype....
However if your using 1.5 you can use generics and it will all be done for you!
-
Mar 9th, 2005, 09:26 PM
#11
Thread Starter
Fanatic Member
Re: Class Design
 Originally Posted by Andy_Hollywood
that what you needed?
Yes, that's it. Thank you very much. I've got this so far.
VB Code:
import java.util.*;
public class DataType{
String sqltype,
javatype,
csharptype,
vbtype;
public DataType(){
}
public DataType(String sqltype,
String javatype,
String csharptype,
String vbtype){
this.sqltype=sqltype;
this.javatype=javatype;
this.csharptype=csharptype;
this.vbtype=vbtype;
}
public ArrayList<DataType> PossibleDataTypes(){
ArrayList<DataType> list=new ArrayList<DataType>();
list.add(new DataType("int","int","int","Integer"));
. . .
return list;
}
}
Feel free to comment guys as I do not know much about this stuff. Thank you.
-
Mar 10th, 2005, 04:10 AM
#12
Addicted Member
Re: Class Design
The only thing i would change, but this is me, is where you specify the java data type i wouldn;t pass in a string, i'd pass in the class, i.e. Integer.class, or String.class, also where you pass in the SQL type i wouldn't pass in a String i'd pass in the int representation of the type from java.sql.Types, i.e. Types.NUMBER, Types.VARCHAR etc..... at least that way you remove the dependancy on the user being able to spell the data types correctly and remove capitalisatin issues etc.....
apart from that, glad it works
Andy
-
Mar 10th, 2005, 04:18 AM
#13
Addicted Member
Re: Class Design
oh yeah ... you might want to seperate out the data type collection from the data type class, i noticed when you call possible data types you recreate the list everytime, might be a little nicer if it created one and kept it around, rather than recreating the same informatio nagain and again.
You could have a public static method on DataType which was getList() which returned a static List of data types to you, then you could add and remove from this in a static block e.g.:
Code:
public class DataType {
protected static List<DataType> dataTypes = new ArrayList<DataType>();
public static List<DataType> getPossibleDataTypes(){
return DataType.dataTypes;
}
static{
DataType.getPossibleDataTypes().add(new DataType(..string..));
DataType.getPossibleDataTypes().add(new DataType(..integer..));
DataType.getPossibleDataTypes().add(new DataType(..byte..));
}
/**
* Original Data Type class
*/
String sqltype,
javatype,
csharptype,
vbtype;
public DataType(){}
public DataType(String sqltype,
String javatype,
String csharptype,
String vbtype){
this.sqltype=sqltype;
this.javatype=javatype;
this.csharptype=csharptype;
this.vbtype=vbtype;
} }
This way you maintain you data type class, but you have a collection of possible data types availbale in the data type class.... you can staically initialise as many data types as you want, but then you also have the ability to add and remove from teh possible data type list from elsewhere because your exposing the list.....
Make sense?
-
Mar 10th, 2005, 04:24 AM
#14
Thread Starter
Fanatic Member
Re: Class Design
w00t
Thanks.
This is just used for my tier generator. The flow is get the information_schema.tables from a certain database and substitute the datatype from sql to java, c# and vb.net. Also to generate classes for the said languages. That's just the reason I made it as a string to write it on a code by code basis.
Say, I have a Customer table, it generates Customer.java providing the variable under the class are the fields from the Customer table. So, just replacing the types by string is (I think) ok for me now.
Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|