nodetypeutil.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 845 行 · 第 1/3 页

JAVA
845
字号
                                    absMin = min;                                }                            }                        }                        if (!maxBoundless) {                            String maxStr = getConstraintMax(constraints[i]);                            if (maxStr == null) {                                maxBoundless = true;                            } else {                                Calendar max = ISO8601.parse(maxStr);                                if (absMax == null || max.after(absMax)) {                                    absMax = max;                                }                            }                        }                    }                    if (satisfied) {                        if (absMin != null) {                            absMin.setTimeInMillis(absMin.getTimeInMillis() + 1);                            if (absMin.after(absMax)) {                                return null;                            }                            return session.getValueFactory().createValue(absMin);                        } else if (absMax != null) {                            absMax.setTimeInMillis(absMax.getTimeInMillis() - 1);                            if (absMax.before(absMin)) {                                return null;                            }                            return session.getValueFactory().createValue(absMax);                        } else {                            // neither min nor max set: return "now"                            return session.getValueFactory().createValue(Calendar.getInstance());                        }                    } else {                        if (!minBoundless) {                            absMin.setTimeInMillis(absMin.getTimeInMillis() - 1);                            return session.getValueFactory().createValue(absMin);                        } else if (!maxBoundless) {                            absMax.setTimeInMillis(absMax.getTimeInMillis() + 1);                            return session.getValueFactory().createValue(absMax);                        } else {                            return null;                        }                    }                }            case (PropertyType.DOUBLE):                {                    double absMin = 0;                    double absMax = 0;                    // indicate if absMin and absMax are already set                    boolean absMinSet = false;                    boolean absMaxSet = false;                    // boundless vars indicate min/max without bounds,                    // if constraint is e.g.(min,) or [,max]                    boolean maxBoundless = false;                    boolean minBoundless = false;                    // find smallest min and largest max value                    for (int i = 0; i < constraints.length; i++) {                        if (!minBoundless) {                            String minStr = getConstraintMin(constraints[i]);                            if (minStr == null) {                                minBoundless = true;                            } else {                                double min = Double.valueOf(minStr).doubleValue();                                if (!absMinSet) {                                    absMin = min;                                    absMinSet = true;                                } else if (min < absMin) {                                    absMin = min;                                }                            }                        }                        if (!maxBoundless) {                            String maxStr = getConstraintMax(constraints[i]);                            if (maxStr == null) {                                maxBoundless = true;                            } else {                                double max = Double.valueOf(maxStr).doubleValue();                                if (!absMaxSet) {                                    absMax = max;                                    absMaxSet = true;                                } else if (max > absMax) {                                    absMax = max;                                }                            }                        }                    }                    if (satisfied) {                        if (minBoundless) {                            return session.getValueFactory().createValue(absMax - 1.0);                        } else if (maxBoundless) {                            return session.getValueFactory().createValue(absMin + 1.0);                        } else if (absMin < absMax) {                            double d = (absMin + absMax) / 2;                            return session.getValueFactory().createValue(d);                        } else {                            return null;                        }                    } else {                        if (!minBoundless) {                            return session.getValueFactory().createValue(absMin - 1.0);                        } else if (!maxBoundless) {                            return session.getValueFactory().createValue(absMax + 1.0);                        } else {                            return null;                        }                    }                }            case (PropertyType.LONG):                {                    long absMin = 0;                    long absMax = 0;                    // indicate if absMin and absMax are already set                    boolean absMinSet = false;                    boolean absMaxSet = false;                    // boundless vars indicate min/max without bounds,                    // if constraint is e.g.(min,) or [,max]                    boolean maxBoundless = false;                    boolean minBoundless = false;                    // find smallest min and largest max value                    for (int i = 0; i < constraints.length; i++) {                        if (!minBoundless) {                            String minStr = getConstraintMin(constraints[i]);                            if (minStr == null) {                                minBoundless = true;                            } else {                                long min = Long.valueOf(minStr).longValue();                                if (!absMinSet) {                                    absMin = min;                                    absMinSet = true;                                } else if (min < absMin) {                                    absMin = min;                                }                            }                        }                        if (!maxBoundless) {                            String maxStr = getConstraintMax(constraints[i]);                            if (maxStr == null) {                                maxBoundless = true;                            } else {                                long max = Long.valueOf(maxStr).longValue();                                if (!absMaxSet) {                                    absMax = max;                                    absMaxSet = true;                                } else if (max > absMax) {                                    absMax = max;                                }                            }                        }                    }                    if (satisfied) {                        if (minBoundless) {                            return session.getValueFactory().createValue(absMax - 1);                        } else if (maxBoundless) {                            return session.getValueFactory().createValue(absMin + 1);                        } else if (absMin < absMax - 1) {                            long x = (absMin + absMax) / 2;                            return session.getValueFactory().createValue(x);                        } else {                            return null;                        }                    } else {                        if (!minBoundless) {                            return session.getValueFactory().createValue(absMin - 1);                        } else if (!maxBoundless) {                            return session.getValueFactory().createValue(absMax + 1);                        } else {                            return null;                        }                    }                }            case (PropertyType.NAME):                {                    if (satisfied) {                        // not in use so far                        return null;                    } else {                        // build a name that is for sure not part of the constraints                        StringBuffer name = new StringBuffer("X");                        for (int i = 0; i < constraints.length; i++) {                            name.append(constraints[i].replaceAll(":", ""));                        }                        return session.getValueFactory().createValue(name.toString(), PropertyType.NAME);                    }                }            case (PropertyType.PATH):                {                    if (satisfied) {                        // not in use so far                        return null;                    } else {                        // build a path that is for sure not part of the constraints                        StringBuffer path = new StringBuffer("X");                        for (int i = 0; i < constraints.length; i++) {                            path.append(constraints[i]);                        }                        String pathStr = path.toString();                        // replace colon to avoid /a/x:b + y:c => /a/x:b:y:c                        // where x:b:y:c is not a legal path element                        pathStr = pathStr.replaceAll(":", "");                        pathStr = pathStr.replaceAll("\\*", "");                        pathStr = pathStr.replaceAll("//", "/");                        return session.getValueFactory().createValue(pathStr, PropertyType.PATH);                    }                }            case (PropertyType.UNDEFINED):                {                    return null;                }            default:                {                    if (satisfied) {                        // not in use so far                        return null;                    } else {                        // build a string that will probably not satisfy the constraints                        StringBuffer value = new StringBuffer("X");                        for (int i = 0; i < constraints.length; i++) {                            value.append(constraints[i]);                        }                        // test if value does not match any of the constraints                        for (int i = 0; i < constraints.length; i++) {                            Pattern pattern = Pattern.compile(constraints[i]);                            Matcher matcher = pattern.matcher(value);                            if (matcher.matches()) {                                return null;                            }                        }                        return session.getValueFactory().createValue(value.toString());                    }                }        }    }    // ------------------------< internal >-------------------------------------    /**     * Get the min value (as string) of a numeric/date constraint string     */    private static String getConstraintMin(String constraint) {        String min = constraint.substring(0, constraint.indexOf(","));        min = min.replaceAll("\\(", "");        min = min.replaceAll("\\[", "");        min = min.replaceAll(" ", "");        if (min.equals("")) {            min = null;        }        return min;    }    /**     * Get the max value (as string) of a numeric/date constraint string     */    private static String getConstraintMax(String constraint) {        String max = constraint.substring(constraint.indexOf(",") + 1);        max = max.replaceAll("\\)", "");        max = max.replaceAll("\\]", "");        max = max.replaceAll(" ", "");        if (max.equals("")) {            max = null;        }        return max;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?