8 Replies - 16502 Views - Last Post: 07 June 2010 - 04:10 AM Rate Topic: -----

#1 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Generic property

Posted 17 September 2007 - 08:47 AM

I tried to write class that could be used like:
	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:
	//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>


Is This A Good Question/Topic? 0
  • +

Replies To: Generic property

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: Generic property

Posted 17 September 2007 - 09:35 AM

which compiler are you using. Java 1.5 supports generics, but some IDEs such as jCreator do not as of yet.
Was This Post Helpful? 0
  • +
  • -

#3 Programmist  Icon User is offline

  • Refactorer in Chief
  • member icon

Reputation: 255
  • View blog
  • Posts: 1,843
  • Joined: 02-January 06

Re: Generic property

Posted 17 September 2007 - 11:25 AM

http://java.sun.com/...e/generics.html
Was This Post Helpful? 0
  • +
  • -

#4 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Generic property

Posted 17 September 2007 - 01:01 PM

View PostWilliam_Wilson, on 17 Sep, 2007 - 07:35 PM, said:

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

View PostProgrammist, on 17 Sep, 2007 - 09:25 PM, said:


"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. :D
Was This Post Helpful? 0
  • +
  • -

#5 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Generic property

Posted 17 September 2007 - 02:25 PM

Anyhow this was what i came up with:
 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!");;
		   }
	   }
	}

Was This Post Helpful? 0
  • +
  • -

#6 Programmist  Icon User is offline

  • Refactorer in Chief
  • member icon

Reputation: 255
  • View blog
  • Posts: 1,843
  • Joined: 02-January 06

Re: Generic property

Posted 17 September 2007 - 02:44 PM

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:

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



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

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 September 2007 - 02:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Generic property

Posted 17 September 2007 - 02:48 PM

View PostProgrammist, on 18 Sep, 2007 - 12:44 AM, said:


:blink: Did not know that :^:

But, i still do not understand why
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 September 2007 - 03:40 PM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_n3whous3*


Reputation:

Re: Generic property

Posted 07 June 2010 - 03:54 AM

Maybe because Java Generics doesn't support primitive types...
Was This Post Helpful? 0

#9 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12178
  • View blog
  • Posts: 45,245
  • Joined: 27-December 08

Re: Generic property

Posted 07 June 2010 - 04:10 AM

I appreciate your willingness to help, but this thread is two or three years old, so I don't think the OP is going to be checking back anytime soon. Please avoid necroposting in the future. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1