I am posting a C# example of extension usage, to make my question more clear. I have a simple class called Parameter with two properties: a key and a value. And I am extending the List of Parameters to be able to check if a certain key is contained in the List:
Parameter Class:
public class Parameter
{
private String key { get; set; }
private String value { get; set; }
}
Parameter Extension Class:
public static class ParameterListExtension
{
public static bool contain(this List<Parameter> parameters, String key)
{
foreach (Parameter parameter in parameters) {
if (parameter.key.Equals(key)) { return true; } }
return false;
}
}
Usage of Extension:
List<Parameter> parameters = getParameters();
if (parameters.contain("myParameter")) { ... }
Can this be done in Java?