Welcome to Dream.In.Code
Become a Java Expert!

Join 149,606 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,901 people online right now. Registration is fast and FREE... Join Now!




Generic property

 
Reply to this topicStart new topic

Generic property

m2s87
17 Sep, 2007 - 07:47 AM
Post #1

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 3 times
Dream Kudos: 1225
My Contributions
I tried to write class that could be used like:
CODE
    public property<int> something = new property<int>(21);
, but failed. So I started wondering, if Java does have generic property type/class or is it limited with:
CODE
    //begin property <PropertyName>
    public <PropertyType> _<PropertyName>;
    public void set<PropertyName>(<PropertyType> NewValue){
       this._<PropertyName>=NewValue;
    }
    public <PropertyType> get<PropertyName>(){
        return this._<PropertyName>;
    }//end property <PropertyName>



User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Generic Property
17 Sep, 2007 - 08:35 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
which compiler are you using. Java 1.5 supports generics, but some IDEs such as jCreator do not as of yet.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Generic Property
17 Sep, 2007 - 10:25 AM
Post #3

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
http://java.sun.com/j2se/1.5.0/docs/guide/...e/generics.html
User is offlineProfile CardPM
+Quote Post

m2s87
RE: Generic Property
17 Sep, 2007 - 12:01 PM
Post #4

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 3 times
Dream Kudos: 1225
My Contributions
QUOTE(William_Wilson @ 17 Sep, 2007 - 07:35 PM) *

which compiler are you using. Java 1.5 supports generics, but some IDEs such as jCreator do not as of yet.

I'm using Eclipse Java Compiler v_686_R32x, 3.2.2 release, on F/(64bit), with what i can set classfile target level: 1.1 to 1.6
QUOTE(Programmist @ 17 Sep, 2007 - 09:25 PM) *

"If you are familiar with C++'s template mechanism, you might think that generics are similar, but the similarity is superficial. Generics do not generate a new class for each specialization, nor do they permit “template metaprogramming.”
I actually have read the document and the tutorial it has link to before more then one time. I have also seen David J. Eck's tutorial, what i would call - en clair. Ahh, probably i was to sluggish to finish writing the class correctly, because i thought - I could use existing type/class. biggrin.gif


User is offlineProfile CardPM
+Quote Post

m2s87
RE: Generic Property
17 Sep, 2007 - 01:25 PM
Post #5

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 3 times
Dream Kudos: 1225
My Contributions
Anyhow this was what i came up with:
CODE
class Property<T> {
       private T _value;
       private State _state=State.Normal;

       public enum State { Normal, ReadOnly, WriteOnly }

       public Property( T a ) {  
           _value = a;
       }
       public Property( T a , State NewState) {  
           _state = NewState;
       }
       public T Get() {
           if(_state  != State.WriteOnly){
               if(_value != null){
                   return _value;
               }else{
                   throw new Error("Variable used before being valuated!");;
               }
           }else{
               throw new Error("Variable is writeonly!");;
           }
       }
       public void Set(T NewValue) {
           if(_state  != State.ReadOnly){
               _value=NewValue;
           }else{
               throw new Error("Variable is readonly!");;
           }
       }
    }

User is offlineProfile CardPM
+Quote Post

Programmist
RE: Generic Property
17 Sep, 2007 - 01:44 PM
Post #6

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Looks good. One "caveat" (if you want to call it that) is trying to instantiate a generic array inside a generic class. Imagine that you wanted to have an array of type T in your class. How do you initialize that array? If you look at the procedure for instantiating generic Collections you might guess that this is how you'd do it:

CODE

private T[] myArray;
...
...
myArray = new T[10];


But this is won't work. You'll have to do this:

CODE

myArray = (T[])new Object[10]


Kinda of a pain in the a$$, but that's how you have to do it. I found this out by inspecting Sun's code for ArrayList because I knew it was backed by a generic array (duh).

This post has been edited by Programmist: 17 Sep, 2007 - 01:46 PM
User is offlineProfile CardPM
+Quote Post

m2s87
RE: Generic Property
17 Sep, 2007 - 01:48 PM
Post #7

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 3 times
Dream Kudos: 1225
My Contributions
QUOTE(Programmist @ 18 Sep, 2007 - 12:44 AM) *


blink.gif Did not know that icon_up.gif

But, i still do not understand why
CODE
public Property<int> something = new Property<int>(32,Property.State.ReadOnly);
//or
public Property<int> something = new Property<int>(21);
gets error " - Syntax error on token "int", Dimensions expected after this token"

And i would probably be using:
ArrayList<Property<int>> myArr = new ArrayList<Property<int>>(); rather then
Property<int>[] myArray=(Property<int>[])new Object[10]

This post has been edited by m2s87: 17 Sep, 2007 - 02:40 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:57PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month