Skip to content

Commit

Permalink
#54 Replace VersionFourGenerator.nextIdentifier() with UUID.randomUUID()
Browse files Browse the repository at this point in the history
It's been there and supported since Java 1.5, while commons-id is something we want to get rid of.
  • Loading branch information
nealeu committed Dec 28, 2023
1 parent ab978eb commit 49cc787
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 59 deletions.
47 changes: 21 additions & 26 deletions core/src/main/java/org/osaf/cosmo/calendar/FreeBusyUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2007 Open Source Applications Foundation
*
*
* 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.
Expand All @@ -17,7 +17,7 @@

import java.util.Iterator;
import java.util.List;

import java.util.UUID;
import net.fortuna.ical4j.model.Parameter;
import net.fortuna.ical4j.model.Period;
import net.fortuna.ical4j.model.PeriodList;
Expand All @@ -31,13 +31,8 @@
import net.fortuna.ical4j.model.property.FreeBusy;
import net.fortuna.ical4j.model.property.Uid;

import org.apache.commons.id.uuid.VersionFourGenerator;

public class FreeBusyUtils {

private static final VersionFourGenerator uuidGenerator =
new VersionFourGenerator();


/**
A VFREEBUSY component overlaps a given time range if the condition
for the corresponding component state specified in the table below
Expand All @@ -46,13 +41,13 @@ public class FreeBusyUtils {
FREEBUSY properties in the absence of DTSTART and DTEND. Any
DURATION property is ignored, as it has a special meaning when
used in a VFREEBUSY component.
When only FREEBUSY properties are used, each period in each
FREEBUSY property is compared against the time range, irrespective
of the type of free busy information (free, busy, busy-tentative,
busy-unavailable) represented by the property.
+------------------------------------------------------+
| VFREEBUSY has both the DTSTART and DTEND properties? |
| +--------------------------------------------------+
Expand All @@ -74,21 +69,21 @@ of the type of free busy information (free, busy, busy-tentative,
* @return true if component overlaps specified range, false otherwise
*/
public static boolean overlapsPeriod(VFreeBusy freeBusy, Period period, TimeZone tz){

DtStart start = freeBusy.getStartDate();
DtEnd end = freeBusy.getEndDate();

if (start != null && end != null) {
InstanceList instances = new InstanceList();
instances.setTimezone(tz);
instances.addComponent(freeBusy, period.getStart(),period.getEnd());
return !instances.isEmpty();
}

PropertyList props = freeBusy.getProperties(Property.FREEBUSY);
if(props.isEmpty())
return false;

Iterator<FreeBusy> it = props.iterator();
while(it.hasNext()) {
FreeBusy fb = it.next();
Expand All @@ -100,10 +95,10 @@ public static boolean overlapsPeriod(VFreeBusy freeBusy, Period period, TimeZone
return true;
}
}

return false;
}

/**
* Merge multiple VFREEBUSY components into a single VFREEBUSY
* component.
Expand All @@ -121,7 +116,7 @@ public static VFreeBusy mergeComponents(List<VFreeBusy> components,
PeriodList busyPeriods = new PeriodList();
PeriodList busyTentativePeriods = new PeriodList();
PeriodList busyUnavailablePeriods = new PeriodList();

for(VFreeBusy vfb: components) {
PropertyList props = vfb.getProperties(Property.FREEBUSY);
for(Iterator it = props.iterator();it.hasNext();) {
Expand All @@ -137,18 +132,18 @@ public static VFreeBusy mergeComponents(List<VFreeBusy> components,
}
}
}

// Merge periods
busyPeriods = busyPeriods.normalise();
busyTentativePeriods = busyTentativePeriods.normalise();
busyUnavailablePeriods = busyUnavailablePeriods.normalise();

// Construct new VFREEBUSY
VFreeBusy vfb =
new VFreeBusy(range.getStart(), range.getEnd());
String uid = uuidGenerator.nextIdentifier().toString();
String uid = UUID.randomUUID().toString();
vfb.getProperties().add(new Uid(uid));

// Add all periods to the VFREEBUSY
if (!busyPeriods.isEmpty()) {
FreeBusy fb = new FreeBusy(busyPeriods);
Expand All @@ -164,8 +159,8 @@ public static VFreeBusy mergeComponents(List<VFreeBusy> components,
fb.getParameters().add(FbType.BUSY_UNAVAILABLE);
vfb.getProperties().add(fb);
}

return vfb;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@
*/
package org.osaf.cosmo.calendar.query.impl;

import net.fortuna.ical4j.model.*;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.ComponentList;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Parameter;
import net.fortuna.ical4j.model.Period;
import net.fortuna.ical4j.model.PeriodList;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.PropertyList;
import net.fortuna.ical4j.model.TimeZone;
import net.fortuna.ical4j.model.component.CalendarComponent;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.component.VFreeBusy;
Expand All @@ -24,19 +36,26 @@
import net.fortuna.ical4j.model.property.Status;
import net.fortuna.ical4j.model.property.Transp;
import net.fortuna.ical4j.model.property.Uid;
import org.apache.commons.id.uuid.VersionFourGenerator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osaf.cosmo.calendar.EntityConverter;
import org.osaf.cosmo.calendar.Instance;
import org.osaf.cosmo.calendar.InstanceList;
import org.osaf.cosmo.calendar.query.*;
import org.osaf.cosmo.calendar.query.CalendarFilter;
import org.osaf.cosmo.calendar.query.CalendarFilterEvaluater;
import org.osaf.cosmo.calendar.query.CalendarQueryProcessor;
import org.osaf.cosmo.calendar.query.ComponentFilter;
import org.osaf.cosmo.calendar.query.TimeRangeFilter;
import org.osaf.cosmo.dao.CalendarDao;
import org.osaf.cosmo.dao.ContentDao;
import org.osaf.cosmo.model.*;

import java.util.HashSet;
import java.util.Set;
import org.osaf.cosmo.model.CalendarCollectionStamp;
import org.osaf.cosmo.model.CollectionItem;
import org.osaf.cosmo.model.ContentItem;
import org.osaf.cosmo.model.HomeCollectionItem;
import org.osaf.cosmo.model.ICalendarItem;
import org.osaf.cosmo.model.Item;
import org.osaf.cosmo.model.StampUtils;
import org.osaf.cosmo.model.User;

/**
* CalendarQueryProcessor implementation that uses CalendarDao.
Expand All @@ -46,8 +65,6 @@ public class StandardCalendarQueryProcessor implements CalendarQueryProcessor {
private static final Log log =
LogFactory.getLog(StandardCalendarQueryProcessor.class);

protected static final VersionFourGenerator uuidGenerator =
new VersionFourGenerator();

private CalendarDao calendarDao = null;
private ContentDao contentDao = null;
Expand Down Expand Up @@ -339,7 +356,7 @@ protected VFreeBusy createVFreeBusy(PeriodList busyPeriods,

// Now create a VFREEBUSY
VFreeBusy vfb = new VFreeBusy(period.getStart(), period.getEnd());
String uid = uuidGenerator.nextIdentifier().toString();
String uid = UUID.randomUUID().toString();
vfb.getProperties().add(new Uid(uid));

// Add all periods to the VFREEBUSY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,41 @@
*/
package org.osaf.cosmo.model.hibernate;

import org.apache.commons.id.IdentifierGenerator;
import org.apache.commons.id.uuid.VersionFourGenerator;
import org.osaf.cosmo.model.*;
import org.w3c.dom.Element;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.UUID;
import org.osaf.cosmo.model.AvailabilityItem;
import org.osaf.cosmo.model.BinaryAttribute;
import org.osaf.cosmo.model.CalendarAttribute;
import org.osaf.cosmo.model.CalendarCollectionStamp;
import org.osaf.cosmo.model.CollectionItem;
import org.osaf.cosmo.model.CollectionSubscription;
import org.osaf.cosmo.model.DecimalAttribute;
import org.osaf.cosmo.model.EntityFactory;
import org.osaf.cosmo.model.EventExceptionStamp;
import org.osaf.cosmo.model.EventStamp;
import org.osaf.cosmo.model.FileItem;
import org.osaf.cosmo.model.FreeBusyItem;
import org.osaf.cosmo.model.IntegerAttribute;
import org.osaf.cosmo.model.MessageStamp;
import org.osaf.cosmo.model.NoteItem;
import org.osaf.cosmo.model.PasswordRecovery;
import org.osaf.cosmo.model.QName;
import org.osaf.cosmo.model.StringAttribute;
import org.osaf.cosmo.model.TaskStamp;
import org.osaf.cosmo.model.TextAttribute;
import org.osaf.cosmo.model.User;
import org.osaf.cosmo.model.XmlAttribute;
import org.w3c.dom.Element;

/**
* EntityFactory implementation that uses Hibernate
* persistent objects.
*/
public class HibEntityFactory implements EntityFactory {

private IdentifierGenerator idGenerator = new VersionFourGenerator();

public CollectionItem createCollection() {
return new HibCollectionItem();
}
Expand Down Expand Up @@ -122,7 +139,7 @@ public User createUser() {
}

public String generateUid() {
return idGenerator.nextIdentifier().toString();
return UUID.randomUUID().toString();
}

}
21 changes: 14 additions & 7 deletions core/src/test/java/org/osaf/cosmo/dao/mock/MockUserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,28 @@
*/
package org.osaf.cosmo.dao.mock;

import org.apache.commons.id.uuid.VersionFourGenerator;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osaf.cosmo.dao.UserDao;
import org.osaf.cosmo.model.*;
import org.osaf.cosmo.model.CollectionSubscription;
import org.osaf.cosmo.model.DuplicateEmailException;
import org.osaf.cosmo.model.DuplicateUsernameException;
import org.osaf.cosmo.model.PasswordRecovery;
import org.osaf.cosmo.model.User;
import org.osaf.cosmo.model.mock.MockAuditableObject;
import org.osaf.cosmo.model.mock.MockUser;
import org.osaf.cosmo.util.ArrayPagedList;
import org.osaf.cosmo.util.PageCriteria;
import org.osaf.cosmo.util.PagedList;

import java.util.*;

/**
* Mock implementation of {@link UserDao} useful for testing.
*/
Expand All @@ -44,8 +53,6 @@ public class MockUserDao implements UserDao {

private MockDaoStorage storage = null;

private VersionFourGenerator idGenerator = new VersionFourGenerator();

/**
*/
public MockUserDao(MockDaoStorage storage) {
Expand Down Expand Up @@ -135,7 +142,7 @@ public User createUser(User user) {
throw new IllegalArgumentException("null user");
}

user.setUid(idGenerator.nextIdentifier().toString());
user.setUid(UUID.randomUUID().toString());

// Set create/modified date, etag for User and associated subscriptions
// and perferences.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,40 @@
*/
package org.osaf.cosmo.model.mock;

import org.apache.commons.id.IdentifierGenerator;
import org.apache.commons.id.uuid.VersionFourGenerator;
import org.osaf.cosmo.model.*;
import org.w3c.dom.Element;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.UUID;
import org.osaf.cosmo.model.AvailabilityItem;
import org.osaf.cosmo.model.BinaryAttribute;
import org.osaf.cosmo.model.CalendarAttribute;
import org.osaf.cosmo.model.CalendarCollectionStamp;
import org.osaf.cosmo.model.CollectionItem;
import org.osaf.cosmo.model.CollectionSubscription;
import org.osaf.cosmo.model.DecimalAttribute;
import org.osaf.cosmo.model.EntityFactory;
import org.osaf.cosmo.model.EventExceptionStamp;
import org.osaf.cosmo.model.EventStamp;
import org.osaf.cosmo.model.FileItem;
import org.osaf.cosmo.model.FreeBusyItem;
import org.osaf.cosmo.model.IntegerAttribute;
import org.osaf.cosmo.model.MessageStamp;
import org.osaf.cosmo.model.NoteItem;
import org.osaf.cosmo.model.PasswordRecovery;
import org.osaf.cosmo.model.QName;
import org.osaf.cosmo.model.StringAttribute;
import org.osaf.cosmo.model.TaskStamp;
import org.osaf.cosmo.model.TextAttribute;
import org.osaf.cosmo.model.User;
import org.osaf.cosmo.model.XmlAttribute;
import org.w3c.dom.Element;

/**
* EntityFactory implementation that uses mock objects.
*/
public class MockEntityFactory implements EntityFactory {

private IdentifierGenerator idGenerator = new VersionFourGenerator();

public CollectionItem createCollection() {
return new MockCollectionItem();
}
Expand Down Expand Up @@ -121,7 +138,7 @@ public User createUser() {
}

public String generateUid() {
return idGenerator.nextIdentifier().toString();
return UUID.randomUUID().toString();
}

}

0 comments on commit 49cc787

Please sign in to comment.