Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to call a function with an enum like argument

12 views
Skip to first unread message

etantonio

unread,
Jan 11, 2009, 8:39:30 AM1/11/09
to
I need to pass as an argument of a function, how I could arrange
this ??


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++

public interface GenericTelemetryType {

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++

public enum AcsTelemetryType implements GenericTelemetryType{

TLM_ACS_RPM ("RPM"),
TLM_ACS_MAGN ("Posizione"),
TLM_ACS_STATO ("Stato"),
TLM_ACS_TEMP ("Temperatura S/A"),
TLM_ACS_CURRENT_MW ("Corrente MW"),
TLM_ACS_VOLTAGE_MW ("Tensione MW"),
TLM_ACS_CURRENT_UP ("Corrente uContr");

private final String comboLabel;

AcsTelemetryType( String comboLabel) {
this.comboLabel = comboLabel;
}

public String getComboLabel() {
return comboLabel;
}

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++

public void test ( GenericTelemetryType genericTelemetryType ){

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++


How I can call the function test passing to it an AcsTelemetryType
enum ???

Thanks,


ANtonio
www.etantonio.it/en

RedGrittyBrick

unread,
Jan 11, 2009, 9:36:30 AM1/11/09
to

etantonio wrote:
> I need to pass as an argument of a function, how I could arrange
> this ??
>

What you want to do is unclear to me.

Perhaps you want to pass a method (i.e. "function") as an argument to
another method.

You can't do this directly in Java. Instead pass to the second method an
instance of an object that has an implementation of the first method.
Usually people define an interface (see ActionListener).

>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> +++
>
> public interface GenericTelemetryType {
>
> }
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> +++
>
> public enum AcsTelemetryType implements GenericTelemetryType{
>
> TLM_ACS_RPM ("RPM"),
> TLM_ACS_MAGN ("Posizione"),
> TLM_ACS_STATO ("Stato"),
> TLM_ACS_TEMP ("Temperatura S/A"),
> TLM_ACS_CURRENT_MW ("Corrente MW"),
> TLM_ACS_VOLTAGE_MW ("Tensione MW"),
> TLM_ACS_CURRENT_UP ("Corrente uContr");
>
> private final String comboLabel;
>
> AcsTelemetryType( String comboLabel) {
> this.comboLabel = comboLabel;
> }
>
> public String getComboLabel() {
> return comboLabel;
> }
>
> }
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> +++
>
> public void test ( GenericTelemetryType genericTelemetryType ){
>
> }

This doesn't appear to be part of a class.

>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> +++
>
>
> How I can call the function test passing to it an AcsTelemetryType
> enum ???
>

The straightforward answer is (for example)
test(TLM_ACS_MAGN);

or maybe
for (AcsTelemetryType acsTelemetryType: AcsTelemetryType.values()) {
test(acsTelemetryType);
}

but I sense that you might want something different. Please clarify.


--
RGB

John B. Matthews

unread,
Jan 11, 2009, 9:46:10 AM1/11/09
to
In article
<7c6e4826-70f0-49f9...@p36g2000prp.googlegroups.com>,
etantonio <postm...@etantonio.it> wrote:

> I need to pass as an argument of a function, how I could arrange

> this? [...] How I can call the function test passing to it a [given]
> enum?

One approach is to extend an adapter for each enumerated function:

<http://sites.google.com/site/drjohnbmatthews/enumerated-functions>

> public String getComboLabel() {
> return comboLabel;
> }

More simply, you can override toString() and use addItem() to add
instances of your enumeration directly to a JComboBox:

<http://sites.google.com/site/drjohnbmatthews/buttons>
<http://sites.google.com/site/drjohnbmatthews/composite>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Daniel Pitts

unread,
Jan 11, 2009, 12:07:20 PM1/11/09
to
Have you tried passing it an AcsTelemetryType enum value?

test(AcsTelemetryType.TLM_ACS_CURRENT_UP);

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

etantonio

unread,
Jan 11, 2009, 1:39:51 PM1/11/09
to
Sorry for bad explaination of the problem :

I need to pass an enum to the function test,
like AcsTelemetryType enum I've also other two different enum :
ObcTelemetryType , RfTelemetryType
so I want that the function test could accept any of these, this is
the reason why I use GenericTelemetryType as interface.
Insiede test I want to bind the enum labels to the combobox, so I need
all the enum, not just one of the values.
How can I solve the problem ??

Thanks

Antonio
www.etantonio.it/de

Patricia Shanahan

unread,
Jan 11, 2009, 4:26:48 PM1/11/09
to

I think the key is the Class object for the enum. You can reference it
directly as e.g. ObcTelemetryType.class.

The class object has a method, getEnumConstants(), that you can use to
get an array of references to the constants.

Patricia

Daniel Pitts

unread,
Jan 12, 2009, 1:11:57 AM1/12/09
to
Do you need all of the values, or even more than that?

if you want to send them one at a time.
for (GenericTelemetryType gtt: AcsTelemtryType.values()) {
test(gtt);

Etantonio

unread,
Jan 12, 2009, 8:02:39 AM1/12/09
to
I need to pass all the enum, not just an element of it,
at the end it seems not possible so I'm trowing away teh enum and
create the corrisponding class.
Thnaks to all anyway

Antonio
www.etantonio.it/fr


Daniel Pitts ha scritto:

Patricia Shanahan

unread,
Jan 12, 2009, 10:13:54 AM1/12/09
to
Etantonio wrote:
> I need to pass all the enum, not just an element of it,
...

I don't see the big distinction you are making between having access to
one element of an enum and to all the elements. If is x is a reference
to some element of an enum, x.getClass().getEnumConstants() is a
reference to an array referencing all the constants in the same enum.

Patricia

Wojtek

unread,
Jan 12, 2009, 10:31:41 AM1/12/09
to
Etantonio wrote :

> I need to pass all the enum, not just an element of it,
> at the end it seems not possible so I'm trowing away teh enum and
> create the corrisponding class.

By definition an enum only resides in one place, so you cannot pass a
reference to it, you can only use it in a static way.

To use/reference more than one element within an enum, use EnumSet.

--
Wojtek :-)


Daniel Pitts

unread,
Jan 12, 2009, 12:40:24 PM1/12/09
to
Etantonio wrote:
> I need to pass all the enum, not just an element of it,
> at the end it seems not possible so I'm trowing away teh enum and
> create the corrisponding class.
> Thnaks to all anyway
An enum is a class, perhaps you're not phrasing your question correctly...

What do you expect to do in the "test()" method?

Lew

unread,
Jan 12, 2009, 1:05:09 PM1/12/09
to
On Jan 12, 10:31 am,

Etantonio wrote :
>> I need to pass all the enum, not just an element of it,
>> at the end it seems not possible so I'm trowing away teh enum and
>> create the corrisponding class.
>

Wojtek wrote:
> By definition an enum only resides in one place, so you cannot pass a
> reference to it, you can only use it in a static way.

Huh?

You most certainly can pass a reference to an enum value. Or do you
mean something different from:

public class Eg
{
public void foo()
{
SomeEnum some = SomeEnum.BAR;
doSomethingWith( some );
}
private void doSomethingWith( SomeEnum val )
{
switch ( val )
{
case BAR:
// etc. ...
}
}
}

That doesn't look like what I would call using the enum value ('some',
'val') "in a static way". Please explain further.

> To use/reference more than one element within an enum, use EnumSet.

Or, if you're using a regular enum and not EnumSet, just use/reference
more than one element of the enum.

--
Lew

Wojtek

unread,
Jan 12, 2009, 4:28:28 PM1/12/09
to
Lew wrote :

> On Jan 12, 10:31 am,
> Etantonio wrote :
>>> I need to pass all the enum, not just an element of it,
>>> at the end it seems not possible so I'm trowing away teh enum and
>>> create the corrisponding class.
>>
>
> Wojtek wrote:
>> By definition an enum only resides in one place, so you cannot pass a
>> reference to it, you can only use it in a static way.
>
> Huh?
>
> You most certainly can pass a reference to an enum value. Or do you
> mean something different from:
>
> public class Eg
> {
> public void foo()
> {
> SomeEnum some = SomeEnum.BAR;
> doSomethingWith( some );
> }
> private void doSomethingWith( SomeEnum val )
> {
> switch ( val )
> {
> case BAR:
> // etc. ...
> }
> }
> }
>
> That doesn't look like what I would call using the enum value ('some',
> 'val') "in a static way". Please explain further.

What I meant is that you cannot instantiate an enum. Somewhere there
can only be a static reference. In your example that would be
SomeEnum.BAR

You cannot have SomeEnum someFoo = new SomeEnum();

Of course you can pass it as a parameter. My bad in not verbalizing
that.

It sounded like the OP wanted to pass the entire enum:
doSomethingWith( SomeEnum );

which needs to be
doSomethingWith( EnumSet.<SomeEnum> allOf(SomeEnum.class) );

--
Wojtek :-)


Lew

unread,
Jan 12, 2009, 4:47:04 PM1/12/09
to
Wojtek wrote:
> What I meant is that you cannot instantiate an enum. Somewhere there
> can only be a static reference. In your example that would be
> SomeEnum.BAR
>
> You cannot have SomeEnum someFoo = new SomeEnum();
>

I see what you mean now.

But "static reference" is a potentially confusing term here. enum
constants are instances just like any other class's instance; a
reference to such an instance is either static or non-static depending
on the declaration of the reference, not the instance. Thus:

enum Bar { BAZ; }
public class Foo
{
Bar bar = BAZ;
}

The element 'bar' of 'Foo' is not a static reference.

However, and I feel sure this is what you really meant, the enum
constants themselves are static references to enum instances. This is
not clear from the phrase "there can only be a static reference."
There can be non-static references to enum instances all over the
place. The constants themselves are static (within the enum class
itself), but other references need not be (within their respective
contexts).

--
Lew

Lew

unread,
Jan 12, 2009, 4:48:58 PM1/12/09
to
Lew wrote:
>  enum Bar { BAZ; }
>  public class Foo
>  {
>    Bar bar = BAZ;

I meant 'Bar.BAZ' of course. Oops.

>  }
>
> The element 'bar' of 'Foo' is not a static reference.

--
Lew

Lew

unread,
Jan 12, 2009, 5:12:39 PM1/12/09
to
Wojtek wrote:
> It sounded like the OP wanted to pass the entire enum:

You mean the entire set of values, I assume.

>   doSomethingWith( SomeEnum );
>
> which needs to be
>   doSomethingWith( EnumSet.<SomeEnum> allOf(SomeEnum.class) );

That is not the only way to pass all the values. One could also use

doSomethingWith( SomeEnum.values() );

(Strictly speaking, the EnumSet version doesn't need to specify the
'<SomeEnum>' as type inference will take care of that.)

<sscce>
package eegee;

import java.util.EnumSet;

enum Foo { FOO, BAR, BAZ; }

/** Fooeynum. */
public class Fooeynum
{
Foo foo = Foo.BAR;

void doSomethingWith( Foo [] vals )
{
System.out.println( "using values" );
for ( Foo val : vals )
{
System.out.println( val.toString() );
}
}

void doSomethingWith( EnumSet<?> vals )
{
System.out.println( "using EnumSet" );
for ( Object val : vals )
{
System.out.println( val.toString() );
}
}

/** Main method.
* @param args <code>String []</code> command line arguments.
*/
public static void main( String [] args )
{
Fooeynum fooey = new Fooeynum();
System.out.println();
fooey.doSomethingWith( Foo.values() );

System.out.println();
fooey.doSomethingWith( EnumSet.allOf( Foo.class ));
}
}
</sscce>
Output:

using values
FOO
BAR
BAZ

using EnumSet
FOO
BAR
BAZ

--
Lew

Roedy Green

unread,
Jan 12, 2009, 7:26:36 PM1/12/09
to
On Sun, 11 Jan 2009 14:36:30 +0000, RedGrittyBrick
<RedGrit...@spamweary.invalid> wrote, quoted or indirectly quoted
someone who said :

>Perhaps you want to pass a method (i.e. "function") as an argument to
>another method.

see http://mindprod.com/jgloss/delegate.html
http://mindprod.com/jgloss/callback.html

if that is what you want to do.
--
Roedy Green Canadian Mind Products
http://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP

Roedy Green

unread,
Jan 12, 2009, 7:32:57 PM1/12/09
to
On Sun, 11 Jan 2009 05:39:30 -0800 (PST), etantonio
<postm...@etantonio.it> wrote, quoted or indirectly quoted someone
who said :

>public void test ( GenericTelemetryType genericTelemetryType ){

Normally you would make test an instance method (overriding the enum
abstract) of the enum constants. Then you could say

GenericTelemetryType.TLM_ACS_STATO.test();


It would make more sense if test were static. Then you could say


test( GenericTelemetryType.TLM_ACS_STATO );

or

GenericTelemetryType g = GenericTelemetryType.TLM_ACS_STATO;

test ( g );


Note they way you have to qualify the enum types even when the
compiler knows perfectly well the enum class the parameter must be.

see http://mindprod.com/jgloss/enum.html

Lew

unread,
Jan 12, 2009, 9:17:04 PM1/12/09
to
Roedy Green wrote:
> On Sun, 11 Jan 2009 05:39:30 -0800 (PST), etantonio
> <postm...@etantonio.it> wrote, quoted or indirectly quoted someone
> who said :
>
>> public void test ( GenericTelemetryType genericTelemetryType ){
>
> Normally you would make test an instance method (overriding the enum
> abstract) of the enum constants. Then you could say
>
> GenericTelemetryType.TLM_ACS_STATO.test();

I don't think that is normal for testing. Normally tests are in separate
classes, e.g., JUnit-derived classes, in a separate source hierarchy that maps
to the same package.

--
Lew

John B. Matthews

unread,
Jan 12, 2009, 9:47:43 PM1/12/09
to
In article <gkgtj0$dda$1...@news.albasani.net>, Lew <no...@lewscanon.com>
wrote:

This would be expected for unit tests of code, but I inferred that the
OP wanted to test, control or perhaps simulate an electro- mechanical
system having measurable quantities such as current, voltage,
temperature and angular frequency. The OP has dismissed several
compelling suggestions. Unfortunately, there's much we don't know about
the problem.

Etantonio

unread,
Jan 13, 2009, 5:33:28 PM1/13/09
to
You are right, I didn't answer to many question, the primary reason is
that I feel I bad explain the problem, so many told me how to send
just an element of the enum hwile I've to pass all the enum because 4
different enums could be sended to the method test, but all that 4
enums implements GenericTelemetryType that have to be in the sign of
the method test.

Functionally yes, I'm reading from the usb (used like a serial with
rxtx library) data coming from a satellite receiver.

Maybe EnumSet could be a good solution for this problem, in any case
as I mentioned before now I'm using a class to model a
telemetryGroup,
inside id I put the enum for the different telemetry, so now the
method test have in the sign an interface implemented from 4 different
telemetryGroup

Thanks to all for the help

Antonio
www.etantonio.it

Mark Space

unread,
Jan 14, 2009, 1:18:46 PM1/14/09
to
Wojtek wrote:
> Etantonio wrote :
>> I need to pass all the enum, not just an element of it,
>> at the end it seems not possible so I'm trowing away teh enum and
>> create the corrisponding class.
>
> By definition an enum only resides in one place, so you cannot pass a
> reference to it, you can only use it in a static way.

Or as Patricia pointed out, you can pass a reference to the enum with
enum.class:

// Pass in the whole thing
printEnums( MyEnum.class );

>
> To use/reference more than one element within an enum, use EnumSet.
>

Or a class literal, as above.

For example, given this method:

static <T extends Enum<T>> void printEnums(
Class<T> aClass )
{
for( Enum<T> e : aClass.getEnumConstants() ) {
System.out.println( e.toString() );
}
}

You can print any enum:

enum Nums {ONE, TWO, THREE};
enum Chars {A, B, C};

printEnums( Nums.class );
printEnums( Chars.class );


(I had to look up the syntax to get this correct, so it's not trivial.
Good to have a reference on generics handy. Don't overlook the method
"getEnumConstants()", which is a Class method, not an Enum method.)

Wojtek

unread,
Jan 14, 2009, 3:08:09 PM1/14/09
to
Mark Space wrote :

> Wojtek wrote:
>> Etantonio wrote :
>>> I need to pass all the enum, not just an element of it,
>>> at the end it seems not possible so I'm trowing away teh enum and
>>> create the corrisponding class.
>>
>> By definition an enum only resides in one place, so you cannot pass a
>> reference to it, you can only use it in a static way.
>

> (I had to look up the syntax to get this correct, so it's not trivial. Good

> to have a reference on generics handy. Don't overlook the method
> "getEnumConstants()", which is a Class method, not an Enum method.)

And you can also make an enum an implementer:

public interface GetID
{
public int getID();
}

....

public enum Count implements GetID
{
ONE(1),TWO(2);

private int myID;

Count(int id)
{
myID=id;
}

public int getID()
{
return myID;
}
}

......

public MyClass
{
public static void doSomething( GetID id )
{
int anID = id.getID();
}
}

.....

MyClass.doSomething(Count.ONE);

--
Wojtek :-)


Wojtek

unread,
Jan 18, 2009, 2:23:31 PM1/18/09
to
Lew wrote :
> Huh?

Yes, yes, I got it the first time around...

--
Wojtek :-)


Lew

unread,
Jan 18, 2009, 3:04:49 PM1/18/09
to
Wojtek wrote:
> Lew wrote :
>> Huh?
>
> Yes, yes, I got it the first time around...

That was the first time around.

--
Lew

0 new messages