From 13a03eb2500b648f556dcd4687f26abbaf87b786 Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Sat, 6 Dec 2014 07:37:47 -0500 Subject: [PATCH] moved withinRange to a utility class Former-commit-id: aab76247ebf674c0e8cdeba56abedb1d510de41c --- .../data/update/StandardUpdate.java | 18 +------- .../owasp/dependencycheck/utils/DateUtil.java | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/DateUtil.java diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/update/StandardUpdate.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/update/StandardUpdate.java index 89a7fc6bd..169f039ca 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/update/StandardUpdate.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/update/StandardUpdate.java @@ -36,6 +36,7 @@ import org.owasp.dependencycheck.data.update.exception.InvalidDataException; import org.owasp.dependencycheck.data.update.exception.UpdateException; import org.owasp.dependencycheck.data.update.task.DownloadTask; import org.owasp.dependencycheck.data.update.task.ProcessTask; +import org.owasp.dependencycheck.utils.DateUtil; import org.owasp.dependencycheck.utils.DownloadFailedException; import org.owasp.dependencycheck.utils.InvalidSettingException; import org.owasp.dependencycheck.utils.Settings; @@ -220,7 +221,7 @@ public class StandardUpdate { final int days = Settings.getInt(Settings.KEYS.CVE_MODIFIED_VALID_FOR_DAYS, 7); if (lastUpdated == updates.getTimeStamp(MODIFIED)) { updates.clear(); //we don't need to update anything. - } else if (withinRange(lastUpdated, now.getTime(), days)) { + } else if (DateUtil.withinDateRange(lastUpdated, now.getTime(), days)) { for (NvdCveInfo entry : updates) { if (MODIFIED.equals(entry.getId())) { entry.setNeedsUpdate(true); @@ -317,19 +318,4 @@ public class StandardUpdate { throw new UpdateException("Error updating the CPE/CVE data, please see the log file for more details."); } } - - /** - * Determines if the epoch date is within the range specified of the compareTo epoch time. This takes the - * (compareTo-date)/1000/60/60/24 to get the number of days. If the calculated days is less then the range the date - * is considered valid. - * - * @param date the date to be checked. - * @param compareTo the date to compare to. - * @param range the range in days to be considered valid. - * @return whether or not the date is within the range. - */ - protected boolean withinRange(long date, long compareTo, int range) { - final double differenceInDays = (compareTo - date) / 1000.0 / 60.0 / 60.0 / 24.0; - return differenceInDays < range; - } } diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/DateUtil.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/DateUtil.java new file mode 100644 index 000000000..9ededbab5 --- /dev/null +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/DateUtil.java @@ -0,0 +1,46 @@ +/* + * This file is part of dependency-check-core. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (c) 2014 Jeremy Long. All Rights Reserved. + */ +package org.owasp.dependencycheck.utils; + +/** + * + * @author Jeremy Long + */ +public final class DateUtil { + + /** + * Private constructor for utility class. + */ + private DateUtil() { + } + + /** + * Determines if the epoch date is within the range specified of the compareTo epoch time. This takes the + * (compareTo-date)/1000/60/60/24 to get the number of days. If the calculated days is less then the range the date + * is considered valid. + * + * @param date the date to be checked. + * @param compareTo the date to compare to. + * @param range the range in days to be considered valid. + * @return whether or not the date is within the range. + */ + public static boolean withinDateRange(long date, long compareTo, int range) { + final double differenceInDays = (compareTo - date) / 1000.0 / 60.0 / 60.0 / 24.0; + return differenceInDays < range; + } +}