Skip to content

Commit

Permalink
Chore: use compiled pattern instead of regular expression that needs …
Browse files Browse the repository at this point in the history
…to be regenerated each time (#1916)

* Chore: use compiled pattern instead of regular expression that needs to be regenerated each time

* Chore: format code
  • Loading branch information
dbmalkovsky authored Dec 26, 2023
1 parent 824ae9a commit d6637e7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
Expand Down Expand Up @@ -124,6 +125,7 @@ public class AdmittedPatientBrowser extends ModalJFrame implements PatientInsert
private static final long serialVersionUID = 1L;

private static final int PANEL_WIDTH = 240;
private static final Pattern DIGIT_PATTERN = Pattern.compile("\\d+");

private PatientHistoryManager patientHistoryManager = Context.getApplicationContext().getBean(PatientHistoryManager.class);

Expand Down Expand Up @@ -1189,15 +1191,15 @@ else if (patientClassBox.getSelectedItem().equals(patientClassItems[1])) {

// lower age limit
String ageLimit = patientAgeFromTextField.getText();
if (ageLimit.matches("\\d+")) {
if (DIGIT_PATTERN.matcher(ageLimit).matches()) {
if (!(ap.getPatient().getAge() >= Integer.parseInt(ageLimit))) {
continue;
}
}

// upper age limit
ageLimit = patientAgeToTextField.getText();
if (ageLimit.matches("\\d+")) {
if (DIGIT_PATTERN.matcher(ageLimit).matches()) {
if (!(ap.getPatient().getAge() <= Integer.parseInt(ageLimit))) {
continue;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/isf/sms/gui/SmsEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.time.LocalDateTime;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;
Expand Down Expand Up @@ -65,6 +66,7 @@
public class SmsEdit extends JDialog implements SelectionListener {

private static final long serialVersionUID = 1L;
private static final Pattern SPACE_PATTERN = Pattern.compile(" ");

private JPanel jCenterPanel;
private JPanel jButtonPanel;
Expand Down Expand Up @@ -231,7 +233,7 @@ private JButton getJOkButton() {
jOkButton = new JButton(MessageBundle.getMessage("angal.common.ok.btn"));
jOkButton.setMnemonic(MessageBundle.getMnemonic("angal.common.ok.btn.key"));
jOkButton.addActionListener(actionEvent -> {
String number = jNumberTextField.getText().replaceAll(" ", "").trim();
String number = SPACE_PATTERN.matcher(jNumberTextField.getText()).replaceAll("").trim();
String text = jTextArea.getText();
LocalDateTime schedDate = jSchedDateChooser.getLocalDateTime();
if (schedDate == null) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/isf/utils/jobjects/VoDoubleTextField.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
package org.isf.utils.jobjects;

import java.util.regex.Pattern;

import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
Expand All @@ -33,6 +35,7 @@
public class VoDoubleTextField extends JTextField {

private static final long serialVersionUID = 1L;
private static final Pattern ALPHA_NUMERIC_PATTERN = Pattern.compile("^[a-zA-Z0-9]*$");

/**
* @param defval - default value
Expand Down Expand Up @@ -70,7 +73,7 @@ public void insertString(int offs, String str, AttributeSet a) throws BadLocatio
Double.parseDouble(newString + '0');
super.insertString(offs, str, a);
} catch (NumberFormatException e) {
if (!str.matches("^[a-zA-Z0-9]*$")) {
if (!ALPHA_NUMERIC_PATTERN.matcher(str).matches()) {
super.insertString(offs, ".", a);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/isf/utils/jobjects/VoFloatTextField.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
package org.isf.utils.jobjects;

import java.util.regex.Pattern;

import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
Expand All @@ -33,6 +35,7 @@
public class VoFloatTextField extends JTextField {

private static final long serialVersionUID = 1L;
private static final Pattern ALPHA_NUMERIC_PATTERN = Pattern.compile("^[a-zA-Z0-9]*$");

/**
* @param defval - default value
Expand Down Expand Up @@ -75,7 +78,7 @@ public void insertString(int offs, String str, AttributeSet a) throws BadLocatio
Float.parseFloat(newString + '0');
super.insertString(offs, str, a);
} catch (NumberFormatException e) {
if (!str.matches("^[a-zA-Z0-9]*$")) {
if (!ALPHA_NUMERIC_PATTERN.matcher(str).matches()) {
super.insertString(offs, ".", a);
}
}
Expand Down

0 comments on commit d6637e7

Please sign in to comment.