I have multiple enums. Here is one example:
public enum Telomeres {
TELOMERE_1("TTAGGG"), TELOMERE_2("TTCAAA");
public String telomere;
Telomeres(String telomere){
this.telomere = telomere;
}
public String getSequence() {
return telomere;
}
}
I am trying to create method (with enum parameter) that loops all values of enum class and checks if String contains at least one of these values. Unfortunately I can't get enum values to check if string contains them. Here is the code, which illustrates problem.
public <T extends Enum<T>> boolean hasTranscriptionStopSite(Class<T> enumData, String chromatin) {
for(T e: enumData.values()) { // I get error here
if(chromatin.contains(e.getSequence())){ // Also I have problems here
hasTranscriptionStopSite = true;
break;
}
}
return hasTranscriptionStopSite;
}
I would be very grateful for any advise how to solve this problem or for correcting my point of view.
To get the enum values from a Class
object, you can use Class.getEnumConstants()
.
If you want access to the getSequence()
function. You should use a interface:
public interface SequenceProvider {
String getSequence();
}
public enum Telomeres implements SequenceProvider {
/*
...
*/
@Override
public String getSequence() {
return telomere;
}
}
and change the signature of hasTranscriptionStopSite(Class, String)
to guarantee, that T
extends the interface:
public <T extends Enum<T> & SequenceProvider> boolean hasTranscriptionStopSite(Class<T> enumData, String chromatin)
Firstly, enumData.values()
is syntax error.
public <T extends Enum<T>> boolean hasTranscriptionStopSite(Class<T> enumData, String chromatin);
^^^
As you can see, enumData
is declared as Class
. the method values()
does not exist within a Class
even though it's a Class<? extends Enum<?>>
Class<? extends Enum<?>>
.
Solution:
Enum<T>
should have been used instead. or;Class<T>
, but replace enumData.values()
with enumData.getEnumConstants()
. Secondly, e.getSequence()
is also syntax error.
for(T e: enumData.values())
^
As you can see here, T
is a generic type which extends Enum<T>
. You can either specify that T
also extends Telomeres
in the declaration of it, or cast instead:
if(e instanceof Telomeres)
((Telomeres) e).getSequence()
As a side note, from your code snippet it seems your function can be condensed within the if()
block, and becomes:
for(T e : enumData.getEnumConstants()) {
if(e instanceof Telomeres)
if(chromatin.contains( ((Telomeres) e).getSequence()) ){
return hasTranscriptionStopSite = true;
Solution using getEnumConstants. Following your example. First you are going to need an interface for the Enum if what you want is to pass different enums to the method hasTranscriptionStopSite:
public interface HasSequence {
String getSequence();
}
The enum implements this interface:
public enum Telomeres implements HasSequence {
TELOMERE_1("TTAGGG"), TELOMERE_2("TTCAAA");
public String telomere;
Telomeres(String telomere){
this.telomere = telomere;
}
public String getSequence() {
return telomere;
}
}
Now you can both get the values from the enum and the sequence from each enum value:
public <T extends Enum<T> & HasSequence> boolean hasTranscriptionStopSite(Class<T> enumData, String chromatin) {
for(T e: enumData.getEnumConstants()) { // THE ONLY CHANGE IS HERE
if(chromatin.contains(e.getSequence())){
hasTranscriptionStopSite = true;
break;
}
}
return hasTranscriptionStopSite;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.