class MipRequest{
private List<String> MipIDs=null;
public List<String> getMipIDs() {
return MipIDs;
}
public void setMipIDs(List<String> mipIDs) {
MipIDs = mipIDs;
}
}
How can i call the get function?
You could call it by using object instance like:
MipRequest mipRequest = new MipRequest();
//set list...
List<String> mipIds = mipRequest.getMipIDs();
//further business logic
Related
I have an abstract class with a single abstract method; and a number of implementing classes (about 6).
The method returns an object that "needs" two parameters.
However, in some cases, only one of the two parameters is required.
Is there an elegant way to implement this case? (instead of return this parameter as empty)
public class NormResult {
protected List<String> normWords;
protected List<String> unNormWords;
public NormResult(List<String> normWords,List<String> unNormWords) {
this.normWords = normWords;
this.unNormWords = unNormWords;
}
public NormResult(List<String> normWords) {
this.normWords = normWords;
this.unNormWords = Collections.emptyList();
}
}
public abstract class AbstractNormalizer {
protected abstract List<NormResult> doNorm();
}
public class FirstNormImpl extends AbstractNormalizer {
protected List<NormResult> doNorm() {
List<String> normWords = new ArrayList<>(5);
List<String> unNormWords = new ArrayList<>(7);
NormResult result = new NormResult(normWords, unNormWords);
return result;
}
}
public class SecondNormImpl extends AbstractNormalizer {
protected List<NormResult> doNorm() {
List<String> normWords = new ArrayList<>(8);
NormResult result = new NormResult(normWords);
return result;
}
}
if you do this to members final:
protected final List<String> normWords;
protected final List<String> unNormWords;
then in the constructor you have to initialize them both... then you can set to an empty collection or a null reference the one you dont have/need
and your overloaded constructor can look like:
public NormResult(List<String> normWords, List<String> unNormWords) {
this.normWords = normWords;
this.unNormWords = unNormWords;
}
public NormResult(List<String> normWords) {
this(normWords, Collections.emptyList());
}
The two changes I would make:
Make the fields final
Use constructor telescoping
as in:
public NormResult(List<String> normWords) {
this(normWords(), Collections.emptyList());
}
to avoid even that simple "code duplication" of assigning values twice.
Beyond that; I agree with the comments; this approach looks reasonable.
I have an app with multiple classes:
MenuActivity, MenuThread, MenuView, MenuBot, MenuBall.
In the class "MenuView" I declare all the ib objects I need:
this.ball = new MenuBall(this, bot1);
this.bot1 = new MenuBot1(this, ball);
this.thread = new MenuThread(this,bot1,ball);
As you can see i didn't create yet the object bot1 but i already use it as a parameter in the object ball, which gives me the error.
Thank you for trying to help me !
You have to change (or add other) constructors of MenuBall and MenuBot1.
Thus, for example:
public class MenuBall {
private MenuBot1 menuBot1;
(...)
// this constructor doesn't need a MenuBot1 object.
public MenuBall(MenuView menuView) {
(...)
}
// setter for the menuBot1
public void setMenuBot1(MenuBot1 menuBot1) {
this.menuBot1 = menuBot1;
}
(...)
}
public class MenuBot1 {
private MenuBall menuBall;
(...)
// this constructor doesn't need a MenuBall object.
public MenuBot1(MenuView menuView) {
(...)
}
// setter for the menuBall
public void setMenuBall(MenuBall menuBall) {
this.menuBall = menuBall;
}
(...)
}
Then in MenuView class:
ball = new MenuBall(this);
bot1 = new MenuBot1(this);
ball.setMenuBot1(bot1);
bot1.setMenuBall(ball);
thread = new MenuThread(this, bot1, ball);
(...)
I have two classes. One that has the array (ArrayStorage) and the other (ArrayConsumer) has just a variable that will act as a simple reference to an array.
I add a new element to the array using $my_array. Then I check to see if the new element is visible in the $obtained_array. But the test fails because it cannot find the new element. They act like they were different arrays. Shouldn't they point to the same array?
public function testArrayMadness() {
$arrayStorage = new ArrayStorage();
$my_array = $arrayStorage->getArray();
$arrayConsumer = new ArrayConsumer($my_array);
$obtained_array = $arrayConsumer->getArray();
$my_array[3]='c';
$this->assertContains('c', $obtained_array);
}
}
class ArrayStorage {
private $my_array=[1=>'a',2=>'b'];
function getArray() { return $this->my_array; }
}
class ArrayConsumer {
private $obtained_array;
function __construct($array) { $this->obtained_array=$array; }
function getArray() { return $this->obtained_array; }
}
Update:
I did the same on test in Java, it gives me an indexOutOfBoundsException. Does that mean both php and java works the same way in this aspect or is there something wrong with my code?
#Test
public void testArrayMadness() {
ArrayStorage arrayStorage = new ArrayStorage();
List<String> my_list = arrayStorage.getList();
ArrayConsumer arrayConsumer = new ArrayConsumer(my_list);
List<String> obtained_array = arrayConsumer.getList();
my_list.add("c");
assertEquals("c", obtained_array.get(3));
}
}
class ArrayStorage {
private List<String> my_list;
public ArrayStorage() {
my_list = new ArrayList<>();
my_list.add("a");
my_list.add("b");
}
public List<String> getList() { return my_list; }
}
class ArrayConsumer {
private List<String> obtained_list;
public ArrayConsumer(List<String> list) {
this.obtained_list = list;
}
public List<String> getList() { return this.obtained_list; }
}
PHP arrays are not objects, they are assigned by value:
$a = [1,2,3];
$b = $a;
$b[2] = 99;
print_r($b); // 1,2,99
print_r($a); // 1,2,3
A workaround is to use reference signs & (a bad idea generally) or ArrayObjects:
$a = new ArrayObject([1,2,3]);
$b = $a;
$b[2] = 99;
print_r($b); // 1,2,99
print_r($a); // 1,2,99
return reference array using & operator
something like return &$this->my_array;
I have a Java class with following implementation:
class Some {
private ArrayList<SomeObject> list = new...
public void addToList(Long t) {
SomeObject so = new SomeObject(new Date, t)
list.add(so)
}
private float fun1(ArrayList<SomeObject> x) {
//some operations on list "list",
//res - result of float calculations based on list "x"
return res
}
public float publicFun() {
//some other operations on private list "list"
return fun1(list);
}
The question is how to test function publicFun() using Mockito, PowerMock or other testing tool ? To run this public function I have to mock private List but how can I do it ?
In this example there are several problems caused by unwelcome dependencies:
1 new Date()
To solve it I suggest to introduce new interface
interface CurrentTimeProvider {
Date getCurrentDate();
}
Implementation is obvious (I skip it for briefness)
2 Is new ArrayList()
You can replace it with you own interface (containing only method you
need)
You can mock ArrayList itself
You can use real impl of ArrayList and test it altogether
In result we get something like this:
class Some {
private CurrentTimeProvider timeProvider;
private ArrayList<SomeObject> list = new ArrayList<SomeObject>();
public void setTimeProvider(CurrentTimeProvider timeProvider) {
this.timeProvider = timeProvider;
}
public void addToList(Long t) {
SomeObject so = new SomeObject(timeProvider.getCurrentDate(), t)
list.add(so)
}
public float publicFun() {
//some other operations on private list "list"
return fun1(list);
}
And test look look this:
CurrentTimeProvider timeProvider = mock(CurrentTimeProvider.class);
Some some = new Some();
some.setTimeProvider(timeProvider);
when(timeProvider.getCurrentDate).thenReturn(mock(Date.class));
//Invoke you method
some.publicFun();
//Put assert and verify here
I have this calss KeywordFilter. I want the constrcutor that accepts a keyword to create a List, add the keyword to the list and then call the constructor with the list parameter. How can I do that? because as I know, calling the constructor should be the first call.
public class KeywordFilter implements Filter {
private List<String> filteringKeywords;
public KeywordFilter(List<String> filteringKeywords) {
this.filteringKeywords = filteringKeywords;
}
public KeywordFilter(String keyword) {
List<String> filteringKeywords = new ArrayList<String>();
filteringKeywords.add(keyword);
this(filteringKeywords);//This makes a compilation error
}
}
Create your list directly :
public KeywordFilter(String keyword) {
this(new ArrayList<String>(Arrays.asList(keyword)));
}
In general, you can put the code that constructs the list in a separate function (preferably, but not necessarily, static):
private static List<String> makeFilterKeywords(String keyword) {
List<String> filteringKeywords = new ArrayList<String>();
filteringKeywords.add(keyword);
return filteringKeywords;
}
public KeywordFilter(String keyword) {
this(makeFilterKeywords(keyword));
}
This should help
public KeywordFilter(String keyword) {
this(Collections.singletonList(keyword));
}
public KeywordFilter(List<String> filteringKeywords) {
this.filteringKeywords = filteringKeywords;
}
public KeywordFilter(String keyword) {
this(((List<String>)Arrays.asList(keyword));
}
The simplest and shorten solution
public KeywordFilter(String keyword) {
this(Arrays.asList(keyword));
}
But this returns a fixed-size list backed by the specified array, without add() or remove() support.
This is applicable also to varargs
public KeywordFilter(String... keywords) {
this(Arrays.asList(keywords));
}
You can create the ArrayList with the KeyWord and then have another method append the new list to existing list (which you have created with only the keyword in the constructor).
Something like this:
public class KeywordFilter implements Filter {
private List<String> filteringKeywords;
public KeywordFilter(String keyword) { //Consctructor
filteringKeywords = new ArrayList<String>();
filteringKeywords.add(keyword);
}
public void appendList(List<String> filteringKeywords) { //new method
filteringKeywords.addAll(filteringKeywords);
}
}