Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a question about param, what param to present the class, such as if I findBuildingsType( buildingEntities, Village) it will return all village building as a result
Welcome.
You can deliver a Class via arguments like this:
private List<Building> findBuildingsType(List<Building> buildings, Class<? extends Building> typ) {
List<Building> re = new ArrayList<>();
for (Building building : buildings) {
if (typ.isInstance(building)) {
re.add(building);
}
}
return re;
}
Especially for your problem consider using streams:
List<Building> buildingsOfTypeMansion = buildings.stream().filter(building -> building instanceof Mansion).collect(Collectors.toList());
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
I would like to ask you about JPA with projection.
Noted: I would like to update new value to getChild.
// From Folder Projection
public interface AuthMenu {
Long getMenuId();
String getMenuName();
String getMenuLink();
List<AuthSubMenu> getChild();
}
// From Folder Service implements
#Override
public List<AuthMenu> getListMenuByRoleUser(Long UserId) {
List<Integer> _actionIds = roleActionsRepository.getListActionIds(UserId);
List<AuthMenu> listMenu = menuRepository.getListMenuIds(1,_actionIds);
for (int i = 0; i < listMenu.size(); i++) {
// Want to update new value <-
}
return listMenu;
}
Could you help me please?
My expectation is want to update new value to getChild().
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
From immutability's point of view, is there any concerns for this code ?
public class mainPkg {
private boolean MyFunc1(MyInfo info) {
List<MyObj> myList = new ArrayList<MyObj>();
anotherPkg.MyFunc2(info, myList);
anotherPkg.MyFunc3(info, myList);
return CollectionUtils.isEmpty(myList);
}
}
public class anotherPkg {
public static boolean MyFunc2(MyInfo info, List<MyObj> myList) {
if(info.version < 2) {
myList.add(new MyObj('wrong version'));
return false;
}
return true;
}
}
MyFunc1(), MyFunc2() are used only in 1 place, and will only be used in this place. When i pass list as argument into myFunc2, it's like the classic call by reference. Not using global variable for myFunc1().
From immutability's point of view, what're are the concerns for this code ?
You are mutating the list referenced by myList in MyFunc2, therefore, your code is not immutable, therefore, it doesn't make sense to look at concerns "from immutability's point of view".
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to write a test case for the given method
private String productCode;
public PaymentManager() {
this.productCode = "SMART-TV;
}
#Override
public boolean isResponsibleFor(TransactionDetailResource resource) {
return productCode.equals(resource.getProductCode());
}
How to write test case for this method
First, create a test class and a test function
#Test
public void testIsResponsibleFor() {
...
}
(If you are using Eclipse, you can press Ctrl+J, It will automatically create the test class for you)
Then in your test function you must create your two objects, one PaymentManager and one TransactionDetailResource.
PaymentManager p = PaymentManager();
TransactionDetailResource t = TransactionDetailResource();
t.setProductCode("SMART-TV")
I have assumed you got a Setter in your TransactionDetailResource class.
AssertThat(p.isResponsibleFor(t),is(Boolean.True))
This is a partial answer but It can gives you a starting point
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I don't find the appropriate solution for my question. As far as I know returning null is a not a good way to write clean code, as the book Clean Code says. However there are many different opinions about this practice, and I am not sure about which one fits on my function.
private EArea getSimilarExistingArea(EImportArea importedArea) {
for (EArea existingArea : exsitingAreas) {
EList<EPoint> importedAreaPoints = importedArea.getPoly().getPoints();
EList<EPoint> existingAreaPoints = existingArea.getPoly().getPoints();
for (EPoint importedAreaPoint : importedAreaPoints) {
for (EPoint existingAreaPoint : existingAreaPoints) {
if (importedAreaPoint.equals(existingAreaPoint))
return existingArea;
}
}
}
return null;
}
What should I return if there is not an existing similar area?
PD: For optimize my code I am breaking the loops with the return if an existing area is founded.
You should take a look at the Optional class!
Make your method return type Optional<EArea> and simply return Optional.ofNullable(existingArea) you will have to slightly modify your Code but the benefits of Optional are really worth it!
Finally I used Optional Class to solve my problem.
Here is the code:
private Optional<EArea> getSimilarExistingArea(EImportArea importedArea) {
for (EArea existingArea : baseLineService.getBaseLine().getAreas()) {
EList<EPoint> importedAreaPoints = importedArea.getPoly().getPoints();
EList<EPoint> existingAreaPoints = existingArea.getPoly().getPoints();
for (EPoint importedAreaPoint : importedAreaPoints) {
for (EPoint existingAreaPoint : existingAreaPoints) {
if (importedAreaPoint.equals(existingAreaPoint))
return Optional.of(existingArea);
}
}
}
return Optional.empty();
}
And here is how I check the returned value:
if (getSimilarExistingArea(importedArea).isPresent())
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
private long payActualMoney;
public long getPayActualMoney() {
return this.payActualMoney;
}
public TAllocate setPayActualMoney(long payActualMoney) {
this.payActualMoney = payActualMoney;
setPayActualMoneyIsSet(true);
return this;
}
PropertyUtils.getPropertyDescriptors not return payActualMoney 's writeMethod,and return an property 'setPayActualMoney'
That is because your setter return type isn't void.