+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++
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
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
> 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>
test(AcsTelemetryType.TLM_ACS_CURRENT_UP);
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
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
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
if you want to send them one at a time.
for (GenericTelemetryType gtt: AcsTelemtryType.values()) {
test(gtt);
Antonio
www.etantonio.it/fr
Daniel Pitts ha scritto:
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
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 :-)
What do you expect to do in the "test()" method?
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
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 :-)
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
I meant 'Bar.BAZ' of course. Oops.
> }
>
> The element 'bar' of 'Foo' is not a static reference.
--
Lew
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
>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
>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
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
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.
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
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.)
> (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 :-)
Yes, yes, I got it the first time around...
--
Wojtek :-)
That was the first time around.
--
Lew