Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import android.widget.TextView;

public class Main {
    /**
     * Checks if a View matches a certain string and returns the amount of total matches.
     * 
     * @param regex the regex to match
     * @param view the view to check
     * @param uniqueTextViews set of views that have matched
     * @return number of total matches
     */

    public static int getNumberOfMatches(String regex, TextView view, Set<TextView> uniqueTextViews) {
        if (view == null) {
            return uniqueTextViews.size();
        }

        Pattern pattern = null;
        try {
            pattern = Pattern.compile(regex);
        } catch (PatternSyntaxException e) {
            pattern = Pattern.compile(regex, Pattern.LITERAL);
        }

        Matcher matcher = pattern.matcher(view.getText().toString());

        if (matcher.find()) {
            uniqueTextViews.add(view);
        }

        if (view.getError() != null) {
            matcher = pattern.matcher(view.getError().toString());
            if (matcher.find()) {
                uniqueTextViews.add(view);
            }
        }
        if (view.getText().toString().equals("") && view.getHint() != null) {
            matcher = pattern.matcher(view.getHint().toString());
            if (matcher.find()) {
                uniqueTextViews.add(view);
            }
        }
        return uniqueTextViews.size();
    }
}