How to optimalize if else statemants with many specifications - java

I am trying to create dynamic search based on fields send in request body.
I prepared many Specifications and in "summary specification" (which is called in method) I want to call them if field is different than null.
It works but the problem is I will never know which parameter will start creating condition so I had to add boolean parameter which resulted in the creation of many if else statements.
Code:
public Specification<ShapeEntity> conditionalSearch(ShapeParams shapeParams) {
Specification spec = null;
boolean isFirstParam = true;
if (shapeParams.getType() != null) {
if (isFirstParam) {
spec = Specification.where(isTypeEqual(shapeParams.getType()));
isFirstParam = false;
} else {
spec = spec.and(isTypeEqual(shapeParams.getType()));
}
}
if (shapeParams.getWidthTo() != null) {
if (isFirstParam) {
spec = Specification.where(isWidthLessThan(shapeParams.getWidthTo()));
isFirstParam = false;
} else {
spec = spec.and(isWidthLessThan(shapeParams.getWidthTo()));
}
}
if (shapeParams.getWidthFrom() != null) {
if (isFirstParam) {
spec = Specification.where(isWidthGreaterThan(shapeParams.getWidthTo()));
isFirstParam = false;
} else {
spec = spec.and(isWidthGreaterThan(shapeParams.getWidthTo()));
}
}
return spec;
}
Is there any way to optimalize it? Specification has to always start with ".where" as first, and next I can add other conditions and I would like to have even 10+ params

You can write some methods that receive some values to validate and return boolean.
boolean checkType(CustomObject type){
return type == null;
}
You can check the use of Optional, it maybe helps with some if blocks.
Optional.ofNullable(type).ifPresent(t -> /*some operations*/);
You can check if you can merge some conditions.
if (shapeParams.getType() != null && isFirstParam) {
//do something....
} else {
//do other....
}

Related

How can i conditionally prevent a user from navigating to a different view/part in eclipse E4 RCP application?

I am trying to prevent a user from going to a different view/part in a perspective of eclipse E4 application.When i am trying to navigate to the same perspective and view,I am facing a stackOverflow exception due to recursively calling the showPart method by the framework.
NavigationHelper.showPerspective(CommonConstants.PERSPECTIVE1, getEclipseContext());
NavigationHelper.showPart(CommonConstants.VIEW1, getEclipseContext());
NavigationHelper.showPart(CommonConstants.VIEW2, getEclipseContext());
My showPart method lokks like this,
public static boolean showPart(String partId, IEclipseContext eclipseContext) {
logger.debug("showPart::STARTED::" + partId);
if (null == eclipseContext) {
eclipseContext = getEclipseContext();
}
if (Model.getInstance().hasDataChanged()) {
if (partId.equalsIgnoreCase(CommonConstants.VIEW1)
|| partId.equalsIgnoreCase(CommonConstants.VIEW2)) {
isNavigationSuccessful = true;
} else {
isNavigationSuccessful = false;
Navigation.showWarning();
}
}
if (isNavigationSuccessful) {
findPartAndActivate(partId, eclipseContext, true);
}
logger.debug("isNavigationSuccessful = " + isNavigationSuccessful);
logger.debug("showPart::END::" + partId);
return isNavigationSuccessful;
}
The findPartAndActivate looks like this
private static boolean findPartAndActivate(String partId, IEclipseContext eclipseContext, boolean giveFocus) {
MTrimmedWindow applicationWindow = ((MTrimmedWindow) ((MApplication) eclipseContext.get(MApplication.class))
.getChildren().get(0));
IEclipseContext currentContext = applicationWindow.getContext();
EPartService partService = currentContext.get(EPartService.class);
EModelService modelService = currentContext.get(EModelService.class);
MPart part = (MPart) modelService.find(partId, eclipseContext.get(MApplication.class).getChildren().get(0));
partService.activate(part, giveFocus);
return true;
}
The partDeactivated is invoked,once a user leaves from a part/View
public void partDeactivated(#Active MPart part) {
if (partInstance.getElementId() != part.getElementId()) {
return;
}
if (transactionButton != null && !transactionButton.isDisposed() && transactionButton.isEnabled()
&& isTransactionCompleted && NavigationHelper.getEditableViewInstance() != null && !partDeactivateFlag) {
doTransaction();
partDeactivateFlag = true;
}
if (Navigation.isPerspective()) {
if (EModel.getInstance().hasDataChanged()/*&& !Model.getInstance().isSwitchFlag()*/) {
System.out.println("Changes");
//Model.getInstance().setSwitchFlag(true);
//partDeactivateFlag = true;
NavigationHelper.showPerspective(CommonConstants.PERSPECTIVE1, getEclipseContext());
NavigationHelper.showPart(CommonConstants.VIEW1, getEclipseContext());
NavigationHelper.showPart(CommonConstants.VIEW2, getEclipseContext());
}
}
viewDeactivated();
}
};
java.lang.StackOverflowError : null
org.eclipse.e4.ui.workbench.modeling.ElementMatcher.select(ElementMatcher.java:71)
org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:182)
org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:317)
org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:271)
org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:271)
org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:271)
org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:251)
org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElementsRecursive(ModelServiceImpl.java:271)
org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElements(ModelServiceImpl.java:428)
org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElements(ModelServiceImpl.java:409)
org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.findElements(ModelServiceImpl.java:414)
org.eclipse.e4.ui.internal.workbench.ModelServiceImpl.find(ModelServiceImpl.java:448)
This is the StackOverflow trace..
How can I solve this..?
Well the stack trace shows this is an error in your code. You are running NavigationHelper.showPart in a part deactivated listener, but your code is causing another part deactivate event which calls the deactivate listener again which calls showPart again and so on.
You can't try to show a different part in the the part deactivate listener directly.
One possibility is to use Display.asyncExec in the part deactivate listener to run the showPart after the deactivate event has completed.

Patching an entity and getting duplicate items

I'm having problems with editing an entity. I have ObjectSpecifications that have different conditions. The problem I have has to do with the DimensionalConditions.
Whenever I edit an ObjectSpecification which has let's say "length: 1", and I try to add "width: 2" to it, the conditions end up being "length: 1, length: 1, width: 2".
Long story short, the conditions that were present before, get inserted again for some reason.
public ObjectSpecification modifySpecification(Long id, ObjectSpecificationRequest request) {
ObjectSpecification objectSpecification = this.getObjectSpecificationById(id);
return this.setSpecData(request, objectSpecification);
}
public ObjectSpecification createSpecification(ObjectSpecificationRequest request){
return this.setSpecData(request, new ObjectSpecification());
}
public void deleteSpecification(Long id) {
objectSpecificationRepository.delete(getObjectSpecificationById(id));
}
public ObjectSpecification setSpecData(ObjectSpecificationRequest request, ObjectSpecification objectSpecification) {
if (request.getName() != null) {
objectSpecification.setName(request.getName());
}
if (request.getDimensionalConditions() != null) {
List<DimensionalCondition> dimensionalConditions = new ArrayList<>();
for (DimensionalCondition condition:request.getDimensionalConditions()) {
DimensionalCondition dimensionalCondition = new DimensionalCondition();
dimensionalCondition.setConditionType(condition.getConditionType());
dimensionalCondition.setValue(condition.getValue());
dimensionalCondition.setObjectSpecification(objectSpecification);
dimensionalConditions.add(dimensionalCondition);
}
objectSpecification.setDimensionalConditions(dimensionalConditions);
}
if (request.getMechanisms() != null) {
objectSpecification.setMechanismConditions(request.getMechanisms());
}
if (request.getServices() != null) {
objectSpecification.setServiceConditions(request.getServices());
}
if (request.getWorkDetails() != null) {
List<WorkDetail> workDetails = new ArrayList<>();
for (WorkDetail workDetail:request.getWorkDetails()) {
workDetails.add(workDetailService.getWorkDetailById(workDetail.getId()));
}
objectSpecification.setWorkDetailConditions(workDetails);
}
return objectSpecificationRepository.save(objectSpecification);
}
1) This is a big update list there, make sure you merge your ObjectSpecification so that it is aligned with the PersistenceContext before you do any amendments.
2) You do not take under consideration the existing DimensionalCondition that are linked to ObjectSpecification. You always create a new one:
List<DimensionalCondition> dimensionalConditions = new ArrayList<>();
3) Make sure you have your cascading configured to {PERSIST, MERGE} at least on #OneToMany List<DimensionalCondition>.

How to check if ARCore lib/apk installed on device?

If I understand well, once ARCore 1.0 will be released on Google Play, it will be necessary to install it on the device in order to be able to run an ARCore app.
How to check if ARCore lib/apk is installed on device ?
Should be sufficient to do something like:
try {
arCoreSession = Session(this)
val config = Config(arCoreSession)
if (!arCoreSession.isSupported(config)) {
Logger.d("ARCore not installed")
} else {
arCoreSession.configure(config)
}
} catch (ex: Throwable) {
Logger.d("ARCore not installed")
}
This is what I'm using here for one my apps and works fine on devices with or wothout ARCore.
According to ARCore documentation 1.4.0, if optional it is important check its availability recursively and then install it:
void maybeEnableArButton() {
// Likely called from Activity.onCreate() of an activity with AR buttons.
ArCoreApk.Availability availability = ArCoreApk.getInstance().checkAvailability(this);
if (availability.isTransient()) {
// re-query at 5Hz while we check compatibility.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
maybeEnableArButton();
}
}, 200);
}
if (availability.isSupported()) {
mArButton.setVisibility(View.VISIBLE);
mArButton.setEnabled(true);
// indicator on the button.
} else { // unsupported or unknown
mArButton.setVisibility(View.INVISIBLE);
mArButton.setEnabled(false);
}
}
If already supported just check if ARCore is installed:
// Set to true ensures requestInstall() triggers installation if necessary.
private boolean mUserRequestedInstall = true;
// in onResume:
try {
if (mSession == null) {
switch (ArCoreApk.getInstance().requestInstall(this, mUserRequestedInstall)) {
case INSTALLED:
mSession = new Session(this);
// Success.
break;
case INSTALL_REQUESTED:
// Ensures next invocation of requestInstall() will either return
// INSTALLED or throw an exception.
mUserRequestedInstall = false;
return;
}
}
} catch (UnavailableUserDeclinedInstallationException e) {
// Display an appropriate message to the user and return gracefully.
return;
} catch (...) { // current catch statements
...
return; // mSession is still null
}
Sometimes it is easier to request this with Rx methodology. Here's the code:
private fun getArAvailabilityRx(context: Context): Single<ArCoreApk.Availability> {
return Single.fromCallable<ArCoreApk.Availability> {
ArCoreApk.getInstance().checkAvailability(context)
}.flatMap { availability ->
if (availability.isTransient) {
// `isTransient` means it hasn't finished loading value; let's request the value in 500 ms
getArAvailabilityRx(context).delaySubscription(500, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
} else {
Single.just(availability)
}
}.observeOn(AndroidSchedulers.mainThread())
}
Here's a little utility class I wrote (based originally on something from https://github.com/google/helloargdx).
It will perform all the checks and setup necessary, in order to ensure it is safe to launch a Session.
abstract class ArCheckFragment : Fragment() {
private var userRequestedInstall = true
abstract fun onCameraPermissionDeny()
abstract fun onArCoreUnavailable(availability: Availability)
abstract fun onArCoreInstallFail(exception: UnavailableException)
abstract fun onArCoreInstallSuccess()
override fun onResume() {
super.onResume()
performCheck()
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_CODE_CAMERA_PERMISSION) {
for (i in permissions.indices) {
if (permissions[i] == Manifest.permission.CAMERA &&
grantResults[i] == PackageManager.PERMISSION_GRANTED
) {
checkArCore()
return
}
}
onCameraPermissionDeny()
}
}
/**
* Performs the whole check
*/
fun performCheck() {
if (requestCameraPermission()) {
checkArCore()
}
}
/**
* Requests the camera permission, if necessary.
* #return whether camera permission is already granted. If so, the permission won't be requested.
*/
private fun requestCameraPermission(): Boolean {
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED
) {
return true
}
requestPermissions(arrayOf(Manifest.permission.CAMERA), REQUEST_CODE_CAMERA_PERMISSION)
return false
}
private fun checkArCore() {
if (!isResumed) {
return
}
val availability = ArCoreApk.getInstance().checkAvailability(activity)
if (availability.isTransient) {
requireView().postDelayed(AR_CORE_CHECK_INTERVAL) { checkArCore() }
return
}
when (availability) {
Availability.SUPPORTED_INSTALLED ->
onArCoreInstallSuccess()
Availability.SUPPORTED_APK_TOO_OLD,
Availability.SUPPORTED_NOT_INSTALLED ->
startArCoreInstallation()
else ->
onArCoreUnavailable(availability)
}
}
private fun startArCoreInstallation() {
try {
val installStatus =
ArCoreApk.getInstance().requestInstall(activity, userRequestedInstall)
when (installStatus) {
InstallStatus.INSTALLED -> onArCoreInstallSuccess()
InstallStatus.INSTALL_REQUESTED,
null ->
// Ensures next invocation of requestInstall() will either return
// INSTALLED or throw an exception.
userRequestedInstall = false
}
} catch (exception: UnavailableException) {
onArCoreInstallFail(exception)
}
}
companion object {
private const val REQUEST_CODE_CAMERA_PERMISSION = 1
private const val AR_CORE_CHECK_INTERVAL = 200L
}
}
You can subclass this Fragment and implement the abstract functions to receive callbacks on what the result of these checks is. Only in onArCoreInstallSuccess is it safe to create a Session.

boolean value not update in Call Receiver

I have call receiver which I want to display dialog on incoming call only. For that I have created a global Boolean variable and trying to changes its value to true in ringing state. But when call disconnects, code always picks default value of Boolean not the updated value given in ringing state. The variable is num. Why it always give false value though its value getting true in ringing state only. Here is the code:
public class phonerece extends BroadcastReceiver{
private Boolean num = false;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
//some task here
}
} else if (extraState != null) {
if (extraState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
//task
} else if (extraState
.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
if (num) {
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//call dialog }
}
} else if (extraState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
if (checknumber() != null) {
Log.e("Nummber", "found");
} else {
Log.e("Number", "Not Found");
num = true;
}
}
}
}
public String checknumber() {
String res = null;
try {
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor c = resolver.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if (c != null) { // cursor not null means number is found contactsTable
if (c.moveToFirst()) { // so now find the contact Name
res = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
}
c.close();
}
} catch (Exception ex) {
/* Ignore */
}
return res;
}
}
You should use static variables (private static num = false) or save your variable in SharedPreferences (it's better), because BroadcastReceivers are not saved between broadcasts. Every broadcast will create a new instance of the BroadcastReceiver, at least if registered automatically via the manifest.
(Your code snippet looks broken, the num variable is missing its type? This answer assumes its type is boolean.)
This sounds like a multithreading problem. Threads in java may cache values of variables, because synchronizing through the main memory is more expensive. You can force the synchronization by flagging the field in question as volatile. This keyword is explained here.
When a field is flagged as volatile, Threads may not cache its value, and all modifications to the variable become visible to all other Threads.
private volatile boolean num = false;

How to disable CustomValidator for a specific scenario

I have a form which uses CustomValidator to check for non empty field whenever we try to Add a record (PARAMETER, VALUE)
I'm looking for a way to disable form validation when I'm trying to Delete (the user can delete an empty listGridRecord if he changes his mind and needs no more to add).
I'm using this custom validator:
CustomValidator validatorParameter = new CustomValidator() {
#Override
protected boolean condition(Object value) {
parameterName = (String) value;
if ((value == null || ((String) value).trim().isEmpty())) {
rowIsValidate = false;
return false;
} else {
rowIsValidate = true;
return true;
}
}
};
which I'm setting in an init() method this way:
parametersListGrid.getField(PARAMETER).setValidators(validatorParameter);
I tried setting a flag "noValidation" on true whenever I detect a click on Delete button and used it this way:
CustomValidator validatorParameter = new CustomValidator() {
#Override
protected boolean condition(Object value) {
parameterName = (String) value;
if (((value == null || ((String) value).trim().isEmpty())) && !noValidation){
rowIsValidate = false;
return false;
} else {
rowIsValidate = true;
return true;
}
}
};
but I figured out that this flag is set later on after the validation happened so
rowIsValidate stays false and we can't delete the empty record given the errors shown after validation;
Any idea on how to pass this validation step just in deletion scenario?
Call discardEdits(rowNum) before deleting a record.
Same question is asked on SmartClient Forums.

Categories

Resources