mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 08:21:25 +00:00
Merge branch 'master' into fix-enumeration-queue
This commit is contained in:
commit
2fb200b705
25 changed files with 3909 additions and 109 deletions
|
@ -213,6 +213,8 @@ NSConnection.m \
|
|||
NSData.m \
|
||||
NSDate.m \
|
||||
NSDateFormatter.m \
|
||||
NSDateInterval.m \
|
||||
NSDateIntervalFormatter.m \
|
||||
NSDebug.m \
|
||||
NSDecimal.m \
|
||||
NSDecimalNumber.m \
|
||||
|
@ -220,6 +222,7 @@ NSDictionary.m \
|
|||
NSDistantObject.m \
|
||||
NSDistributedLock.m \
|
||||
NSDistributedNotificationCenter.m \
|
||||
NSEnergyFormatter.m \
|
||||
NSEnumerator.m \
|
||||
NSError.m \
|
||||
NSException.m \
|
||||
|
@ -244,10 +247,14 @@ NSKeyedArchiver.m \
|
|||
NSKeyedUnarchiver.m \
|
||||
NSKeyValueCoding.m \
|
||||
NSKeyValueObserving.m \
|
||||
NSLengthFormatter.m \
|
||||
NSLocale.m \
|
||||
NSLock.m \
|
||||
NSLog.m \
|
||||
NSMapTable.m \
|
||||
NSMassFormatter.m \
|
||||
NSMeasurementFormatter.m \
|
||||
NSMeasurement.m \
|
||||
NSMetadata.m \
|
||||
NSMethodSignature.m \
|
||||
NSNotification.m \
|
||||
|
@ -292,6 +299,7 @@ NSScriptKeyValueCoding.m \
|
|||
NSScriptObjectSpecifiers.m \
|
||||
NSScriptStandardSuiteCommands.m \
|
||||
NSScriptSuiteRegistry.m \
|
||||
NSUnit.m \
|
||||
NSUserScriptTask.m \
|
||||
NSSerializer.m \
|
||||
NSSet.m \
|
||||
|
@ -401,6 +409,8 @@ NSCompoundPredicate.h \
|
|||
NSConnection.h \
|
||||
NSData.h \
|
||||
NSDateFormatter.h \
|
||||
NSDateInterval.h \
|
||||
NSDateIntervalFormatter.h \
|
||||
NSDate.h \
|
||||
NSDebug.h \
|
||||
NSDecimal.h \
|
||||
|
@ -409,6 +419,7 @@ NSDictionary.h \
|
|||
NSDistantObject.h \
|
||||
NSDistributedLock.h \
|
||||
NSDistributedNotificationCenter.h \
|
||||
NSEnergyFormatter.h \
|
||||
NSEnumerator.h \
|
||||
NSError.h \
|
||||
NSErrorRecoveryAttempting.h \
|
||||
|
@ -435,9 +446,13 @@ NSJSONSerialization.h \
|
|||
NSKeyedArchiver.h \
|
||||
NSKeyValueCoding.h \
|
||||
NSKeyValueObserving.h \
|
||||
NSLengthFormatter.h \
|
||||
NSLocale.h \
|
||||
NSLock.h \
|
||||
NSMapTable.h \
|
||||
NSMassFormatter.h \
|
||||
NSMeasurementFormatter.h \
|
||||
NSMeasurement.h \
|
||||
NSMetadata.h \
|
||||
NSMethodSignature.h \
|
||||
NSNetServices.h \
|
||||
|
@ -477,6 +492,7 @@ NSScriptKeyValueCoding.h \
|
|||
NSScriptObjectSpecifiers.h \
|
||||
NSScriptStandardSuiteCommands.h \
|
||||
NSScriptSuiteRegistry.h \
|
||||
NSUnit.h \
|
||||
NSUserScriptTask.h \
|
||||
NSScriptWhoseTests.h \
|
||||
NSSerialization.h \
|
||||
|
|
|
@ -159,7 +159,7 @@ otherTime(NSDate* other)
|
|||
return NSAllocateObject(self, 0, z);
|
||||
}
|
||||
|
||||
+ (id) date
|
||||
+ (instancetype) date
|
||||
{
|
||||
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
||||
initWithTimeIntervalSinceReferenceDate: GSPrivateTimeNow()]);
|
||||
|
@ -171,14 +171,14 @@ otherTime(NSDate* other)
|
|||
* specified by the preferences in the user defaults database, allowing
|
||||
* phrases like 'last tuesday'
|
||||
*/
|
||||
+ (id) dateWithNaturalLanguageString: (NSString*)string
|
||||
+ (instancetype) dateWithNaturalLanguageString: (NSString*)string
|
||||
{
|
||||
return [self dateWithNaturalLanguageString: string
|
||||
locale: nil];
|
||||
}
|
||||
|
||||
+ (id) dateWithNaturalLanguageString: (NSString*)string
|
||||
locale: (NSDictionary*)locale
|
||||
+ (instancetype) dateWithNaturalLanguageString: (NSString*)string
|
||||
locale: (NSDictionary*)locale
|
||||
{
|
||||
NSCharacterSet *ws;
|
||||
NSCharacterSet *digits;
|
||||
|
@ -885,35 +885,36 @@ otherTime(NSDate* other)
|
|||
}
|
||||
}
|
||||
|
||||
+ (id) dateWithString: (NSString*)description
|
||||
+ (instancetype) dateWithString: (NSString*)description
|
||||
{
|
||||
return AUTORELEASE([[self alloc] initWithString: description]);
|
||||
}
|
||||
|
||||
+ (id) dateWithTimeInterval: (NSTimeInterval)seconds sinceDate: (NSDate*)date
|
||||
+ (instancetype) dateWithTimeInterval: (NSTimeInterval)seconds
|
||||
sinceDate: (NSDate*)date
|
||||
{
|
||||
return AUTORELEASE([[self alloc] initWithTimeInterval: seconds
|
||||
sinceDate: date]);
|
||||
}
|
||||
|
||||
+ (id) dateWithTimeIntervalSince1970: (NSTimeInterval)seconds
|
||||
+ (instancetype) dateWithTimeIntervalSince1970: (NSTimeInterval)seconds
|
||||
{
|
||||
return AUTORELEASE([[self alloc] initWithTimeIntervalSinceReferenceDate:
|
||||
seconds - NSTimeIntervalSince1970]);
|
||||
}
|
||||
|
||||
+ (id) dateWithTimeIntervalSinceNow: (NSTimeInterval)seconds
|
||||
+ (instancetype) dateWithTimeIntervalSinceNow: (NSTimeInterval)seconds
|
||||
{
|
||||
return AUTORELEASE([[self alloc] initWithTimeIntervalSinceNow: seconds]);
|
||||
}
|
||||
|
||||
+ (id) dateWithTimeIntervalSinceReferenceDate: (NSTimeInterval)seconds
|
||||
+ (instancetype) dateWithTimeIntervalSinceReferenceDate: (NSTimeInterval)seconds
|
||||
{
|
||||
return AUTORELEASE([[self alloc] initWithTimeIntervalSinceReferenceDate:
|
||||
seconds]);
|
||||
}
|
||||
|
||||
+ (id) distantPast
|
||||
+ (instancetype) distantPast
|
||||
{
|
||||
if (_distantPast == nil)
|
||||
{
|
||||
|
@ -922,7 +923,7 @@ otherTime(NSDate* other)
|
|||
return _distantPast;
|
||||
}
|
||||
|
||||
+ (id) distantFuture
|
||||
+ (instancetype) distantFuture
|
||||
{
|
||||
if (_distantFuture == nil)
|
||||
{
|
||||
|
@ -940,7 +941,7 @@ otherTime(NSDate* other)
|
|||
return GSPrivateTimeNow();
|
||||
}
|
||||
|
||||
- (id) addTimeInterval: (NSTimeInterval)seconds
|
||||
- (instancetype) addTimeInterval: (NSTimeInterval)seconds
|
||||
{
|
||||
return [self dateByAddingTimeInterval: seconds];
|
||||
}
|
||||
|
@ -976,7 +977,7 @@ otherTime(NSDate* other)
|
|||
return abstractClass;
|
||||
}
|
||||
|
||||
- (id) dateByAddingTimeInterval: (NSTimeInterval)ti
|
||||
- (instancetype) dateByAddingTimeInterval: (NSTimeInterval)ti
|
||||
{
|
||||
return [[self class] dateWithTimeIntervalSinceReferenceDate:
|
||||
otherTime(self) + ti];
|
||||
|
@ -1069,7 +1070,7 @@ otherTime(NSDate* other)
|
|||
return (NSUInteger)[self timeIntervalSinceReferenceDate];
|
||||
}
|
||||
|
||||
- (id) initWithCoder: (NSCoder*)coder
|
||||
- (instancetype) initWithCoder: (NSCoder*)coder
|
||||
{
|
||||
NSTimeInterval interval;
|
||||
id o;
|
||||
|
@ -1099,12 +1100,12 @@ otherTime(NSDate* other)
|
|||
return o;
|
||||
}
|
||||
|
||||
- (id) init
|
||||
- (instancetype) init
|
||||
{
|
||||
return [self initWithTimeIntervalSinceReferenceDate: GSPrivateTimeNow()];
|
||||
}
|
||||
|
||||
- (id) initWithString: (NSString*)description
|
||||
- (instancetype) initWithString: (NSString*)description
|
||||
{
|
||||
// Easiest to just have NSCalendarDate do the work for us
|
||||
NSCalendarDate *d = [calendarClass alloc];
|
||||
|
@ -1123,8 +1124,8 @@ otherTime(NSDate* other)
|
|||
}
|
||||
}
|
||||
|
||||
- (id) initWithTimeInterval: (NSTimeInterval)secsToBeAdded
|
||||
sinceDate: (NSDate*)anotherDate
|
||||
- (instancetype) initWithTimeInterval: (NSTimeInterval)secsToBeAdded
|
||||
sinceDate: (NSDate*)anotherDate
|
||||
{
|
||||
if (anotherDate == nil)
|
||||
{
|
||||
|
@ -1137,20 +1138,20 @@ otherTime(NSDate* other)
|
|||
otherTime(anotherDate) + secsToBeAdded];
|
||||
}
|
||||
|
||||
- (id) initWithTimeIntervalSince1970: (NSTimeInterval)seconds
|
||||
- (instancetype) initWithTimeIntervalSince1970: (NSTimeInterval)seconds
|
||||
{
|
||||
return [self initWithTimeIntervalSinceReferenceDate:
|
||||
seconds - NSTimeIntervalSince1970];
|
||||
}
|
||||
|
||||
- (id) initWithTimeIntervalSinceNow: (NSTimeInterval)secsToBeAdded
|
||||
- (instancetype) initWithTimeIntervalSinceNow: (NSTimeInterval)secsToBeAdded
|
||||
{
|
||||
// Get the current time, add the secs and init thyself
|
||||
return [self initWithTimeIntervalSinceReferenceDate:
|
||||
GSPrivateTimeNow() + secsToBeAdded];
|
||||
}
|
||||
|
||||
- (id) initWithTimeIntervalSinceReferenceDate: (NSTimeInterval)secs
|
||||
- (instancetype) initWithTimeIntervalSinceReferenceDate: (NSTimeInterval)secs
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return self;
|
||||
|
|
221
Source/NSDateInterval.m
Normal file
221
Source/NSDateInterval.m
Normal file
|
@ -0,0 +1,221 @@
|
|||
/* Implementation of class NSDateInterval
|
||||
Copyright (C) 2019 Free Software Foundation, Inc.
|
||||
|
||||
By: Gregory Casamento <greg.casamento@gmail.com>
|
||||
Date: Wed Oct 9 16:24:13 EDT 2019
|
||||
|
||||
This file is part of the GNUstep Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
*/
|
||||
|
||||
#include <Foundation/NSDateInterval.h>
|
||||
#include <Foundation/NSDate.h>
|
||||
#include <Foundation/NSArray.h>
|
||||
#include <Foundation/NSException.h>
|
||||
|
||||
@implementation NSDateInterval
|
||||
|
||||
// Init
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if(self != nil)
|
||||
{
|
||||
_startDate = [NSDate date];
|
||||
_duration = 0.0;
|
||||
RETAIN(_startDate);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithStartDate:(NSDate *)startDate
|
||||
duration:(NSTimeInterval)duration
|
||||
{
|
||||
self = [super init];
|
||||
if(self != nil)
|
||||
{
|
||||
ASSIGNCOPY(_startDate, startDate);
|
||||
if(duration < 0)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"Duration %f is less than zero", duration];
|
||||
}
|
||||
_duration = duration;
|
||||
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithStartDate:(NSDate *)startDate
|
||||
endDate:(NSDate *)endDate
|
||||
{
|
||||
return [self initWithStartDate: startDate
|
||||
duration: [endDate timeIntervalSinceDate: startDate]];
|
||||
}
|
||||
|
||||
- (instancetype) initWithCoder: (NSCoder *)coder
|
||||
{
|
||||
// TODO: Implement encoding
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void) encodeWithCoder: (NSCoder *)coder
|
||||
{
|
||||
}
|
||||
|
||||
- (id) copyWithZone: (NSZone *)zone
|
||||
{
|
||||
return [[[self class] allocWithZone: zone]
|
||||
initWithStartDate: _startDate
|
||||
duration: _duration];
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
RELEASE(_startDate);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
// Access
|
||||
- (NSDate *) startDate
|
||||
{
|
||||
return _startDate;
|
||||
}
|
||||
|
||||
- (void) setStartDate: (NSDate *)startDate
|
||||
{
|
||||
ASSIGNCOPY(_startDate, startDate);
|
||||
}
|
||||
|
||||
- (NSDate *) endDate
|
||||
{
|
||||
return [_startDate dateByAddingTimeInterval: _duration];
|
||||
}
|
||||
|
||||
- (void) setEndDate: (NSDate *)endDate
|
||||
{
|
||||
_duration = [endDate timeIntervalSinceDate: _startDate];
|
||||
}
|
||||
|
||||
- (NSTimeInterval) duration
|
||||
{
|
||||
return _duration;
|
||||
}
|
||||
|
||||
- (void) setDuration: (NSTimeInterval)duration
|
||||
{
|
||||
_duration = duration;
|
||||
}
|
||||
|
||||
// Compare
|
||||
- (NSComparisonResult) compare: (NSDateInterval *)dateInterval
|
||||
{
|
||||
NSComparisonResult result = NSOrderedSame;
|
||||
|
||||
if([_startDate isEqualToDate: [dateInterval startDate]] &&
|
||||
_duration < [dateInterval duration])
|
||||
{
|
||||
result = NSOrderedAscending;
|
||||
}
|
||||
else if([_startDate compare: [dateInterval startDate]] == NSOrderedAscending)
|
||||
{
|
||||
result = NSOrderedAscending;
|
||||
}
|
||||
else if([self isEqualToDateInterval: dateInterval])
|
||||
{
|
||||
result = NSOrderedSame;
|
||||
}
|
||||
else if([_startDate isEqualToDate: [dateInterval startDate]] &&
|
||||
_duration > [dateInterval duration])
|
||||
{
|
||||
result = NSOrderedDescending;
|
||||
}
|
||||
else if([_startDate compare: [dateInterval startDate]] == NSOrderedDescending)
|
||||
{
|
||||
result = NSOrderedDescending;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
- (BOOL) isEqualToDateInterval: (NSDateInterval *)dateInterval
|
||||
{
|
||||
return ([_startDate isEqualToDate: [dateInterval startDate]] &&
|
||||
_duration == [dateInterval duration]);
|
||||
}
|
||||
|
||||
// Determine
|
||||
- (BOOL) intersectsDateInterval: (NSDateInterval *)dateInterval
|
||||
{
|
||||
return [self intersectionWithDateInterval: dateInterval] != nil;
|
||||
}
|
||||
|
||||
- (NSDateInterval *) intersectionWithDateInterval: (NSDateInterval *)dateInterval
|
||||
{
|
||||
NSDateInterval *result = nil;
|
||||
NSDateInterval *first = self; //[sortedArray firstObject];
|
||||
NSDateInterval *last = dateInterval; // [sortedArray lastObject];
|
||||
NSDate *intersectStartDate = nil;
|
||||
NSDate *intersectEndDate = nil;
|
||||
// NSArray *array = [NSArray arrayWithObjects: self, dateInterval, nil];
|
||||
// NSArray *sortedArray = [array sortedArrayUsingSelector: @selector(compare:)];
|
||||
|
||||
// Max of start date....
|
||||
if([[first startDate] compare: [last startDate]] == NSOrderedAscending ||
|
||||
[[first startDate] isEqualToDate: [last startDate]])
|
||||
{
|
||||
intersectStartDate = [last startDate];
|
||||
}
|
||||
if([[first startDate] compare: [last startDate]] == NSOrderedDescending)
|
||||
{
|
||||
intersectStartDate = [first startDate];
|
||||
}
|
||||
|
||||
// Min of end date...
|
||||
if([[first endDate] compare: [last endDate]] == NSOrderedDescending ||
|
||||
[[first endDate] isEqualToDate: [last endDate]])
|
||||
{
|
||||
intersectEndDate = [last endDate];
|
||||
}
|
||||
if([[first endDate] compare: [last endDate]] == NSOrderedAscending)
|
||||
{
|
||||
intersectEndDate = [first endDate];
|
||||
}
|
||||
|
||||
if([intersectStartDate compare: intersectEndDate] == NSOrderedAscending)
|
||||
{
|
||||
result = [[NSDateInterval alloc] initWithStartDate: intersectStartDate
|
||||
endDate: intersectEndDate];
|
||||
AUTORELEASE(result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Contain
|
||||
- (BOOL) containsDate: (NSDate *)date
|
||||
{
|
||||
NSDate *endDate = [self endDate];
|
||||
return ([_startDate compare: date] == NSOrderedSame ||
|
||||
[endDate compare: date] == NSOrderedSame ||
|
||||
([_startDate compare: date] == NSOrderedAscending &&
|
||||
[endDate compare: date] == NSOrderedDescending));
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
|
115
Source/NSDateIntervalFormatter.m
Normal file
115
Source/NSDateIntervalFormatter.m
Normal file
|
@ -0,0 +1,115 @@
|
|||
/* Implementation of class NSDateIntervalFormatter
|
||||
Copyright (C) 2019 Free Software Foundation, Inc.
|
||||
|
||||
By: Gregory John Casamento <greg.casamento@gmail.com>
|
||||
Date: Wed Oct 9 16:23:55 EDT 2019
|
||||
|
||||
This file is part of the GNUstep Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
*/
|
||||
|
||||
#include <Foundation/NSDateIntervalFormatter.h>
|
||||
#include <Foundation/NSLocale.h>
|
||||
#include <Foundation/NSCalendar.h>
|
||||
#include <Foundation/NSTimeZone.h>
|
||||
#include <Foundation/NSString.h>
|
||||
#include <Foundation/NSDate.h>
|
||||
#include <Foundation/NSDateInterval.h>
|
||||
|
||||
@implementation NSDateIntervalFormatter
|
||||
// Properties
|
||||
- (NSLocale *) locale
|
||||
{
|
||||
return _locale;
|
||||
}
|
||||
|
||||
- (void) setLocale: (NSLocale *)locale
|
||||
{
|
||||
ASSIGNCOPY(_locale, locale);
|
||||
}
|
||||
|
||||
- (NSCalendar *) calendar
|
||||
{
|
||||
return _calendar;
|
||||
}
|
||||
|
||||
- (void) setCalendar: (NSCalendar *)calendar
|
||||
{
|
||||
ASSIGNCOPY(_calendar, calendar);
|
||||
}
|
||||
|
||||
- (NSTimeZone *) timeZone
|
||||
{
|
||||
return _timeZone;
|
||||
}
|
||||
|
||||
- (void) setTimeZone: (NSTimeZone *)timeZone
|
||||
{
|
||||
ASSIGNCOPY(_timeZone, timeZone);
|
||||
}
|
||||
|
||||
- (NSString *) dateTemplate
|
||||
{
|
||||
return _dateTemplate;
|
||||
}
|
||||
|
||||
- (void) setDateTemplate: (NSString *)dateTemplate
|
||||
{
|
||||
ASSIGNCOPY(_dateTemplate, dateTemplate);
|
||||
}
|
||||
|
||||
- (NSDateIntervalFormatterStyle) dateStyle
|
||||
{
|
||||
return _dateStyle;
|
||||
}
|
||||
|
||||
- (void) setDateStyle: (NSDateIntervalFormatterStyle)dateStyle
|
||||
{
|
||||
_dateStyle = dateStyle;
|
||||
}
|
||||
|
||||
- (NSDateIntervalFormatterStyle) timeStyle
|
||||
{
|
||||
return _timeStyle;
|
||||
}
|
||||
|
||||
- (void) setTimeStyle: (NSDateIntervalFormatterStyle)timeStyle
|
||||
{
|
||||
_timeStyle = timeStyle;
|
||||
}
|
||||
|
||||
// Create strings
|
||||
- (NSString *) stringFromDate: (NSDate *)fromDate toDate: (NSDate *)toDate
|
||||
{
|
||||
NSDateInterval *interval = [[NSDateInterval alloc] initWithStartDate: fromDate
|
||||
endDate: toDate];
|
||||
AUTORELEASE(interval);
|
||||
return [self stringFromDateInterval: interval];
|
||||
}
|
||||
|
||||
- (NSString *) stringFromDateInterval: (NSDateInterval *)dateInterval
|
||||
{
|
||||
NSDate *fromDate = [dateInterval startDate];
|
||||
NSDate *toDate = [dateInterval endDate];
|
||||
|
||||
// Add formatting of NSDate here.
|
||||
|
||||
return [NSString stringWithFormat: @"%@ - %@", fromDate, toDate];
|
||||
}
|
||||
|
||||
@end
|
||||
|
128
Source/NSEnergyFormatter.m
Normal file
128
Source/NSEnergyFormatter.m
Normal file
|
@ -0,0 +1,128 @@
|
|||
/* Implementation of class NSEnergyFormatter
|
||||
Copyright (C) 2019 Free Software Foundation, Inc.
|
||||
|
||||
By: Gregory John Casamento <greg.casamento@gmail.com>
|
||||
Date: Tue Oct 8 13:30:10 EDT 2019
|
||||
|
||||
This file is part of the GNUstep Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
*/
|
||||
|
||||
#include <Foundation/NSEnergyFormatter.h>
|
||||
#include <Foundation/NSMeasurement.h>
|
||||
#include <Foundation/NSMeasurementFormatter.h>
|
||||
#include <Foundation/NSUnit.h>
|
||||
|
||||
@implementation NSEnergyFormatter
|
||||
|
||||
- (instancetype) init
|
||||
{
|
||||
self = [super init];
|
||||
if(self != nil)
|
||||
{
|
||||
_numberFormatter = nil;
|
||||
_unitStyle = NSFormattingUnitStyleMedium;
|
||||
_isForFoodEnergyUse = NO;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSNumberFormatter *) numberFormatter
|
||||
{
|
||||
return _numberFormatter;
|
||||
}
|
||||
|
||||
- (void) setNumberFormatter: (NSNumberFormatter *)formatter
|
||||
{
|
||||
ASSIGN(_numberFormatter, formatter);
|
||||
}
|
||||
|
||||
- (NSFormattingUnitStyle) unitStyle
|
||||
{
|
||||
return _unitStyle;
|
||||
}
|
||||
|
||||
- (void) setUnitStyle: (NSFormattingUnitStyle)style
|
||||
{
|
||||
_unitStyle = style;
|
||||
}
|
||||
|
||||
- (BOOL) isForFoodEnergyUse
|
||||
{
|
||||
return _isForFoodEnergyUse;
|
||||
}
|
||||
|
||||
- (void) setForFoodEnergyUse: (BOOL)flag
|
||||
{
|
||||
_isForFoodEnergyUse = flag;
|
||||
}
|
||||
- (NSString *) stringFromValue: (double)value unit: (NSEnergyFormatterUnit)unit
|
||||
{
|
||||
NSUnit *u = nil;
|
||||
NSMeasurement *m = nil;
|
||||
NSMeasurementFormatter *mf = nil;
|
||||
|
||||
switch(unit)
|
||||
{
|
||||
case NSEnergyFormatterUnitJoule:
|
||||
u = [NSUnitEnergy joules];
|
||||
break;
|
||||
case NSEnergyFormatterUnitKilojoule:
|
||||
u = [NSUnitEnergy kilojoules];
|
||||
break;
|
||||
case NSEnergyFormatterUnitCalorie:
|
||||
u = [NSUnitEnergy calories];
|
||||
break;
|
||||
case NSEnergyFormatterUnitKilocalorie:
|
||||
u = [NSUnitEnergy kilocalories];
|
||||
break;
|
||||
}
|
||||
|
||||
m = [[NSMeasurement alloc] initWithDoubleValue: value
|
||||
unit: u];
|
||||
AUTORELEASE(m);
|
||||
mf = [[NSMeasurementFormatter alloc] init];
|
||||
AUTORELEASE(mf);
|
||||
[mf setUnitStyle: _unitStyle];
|
||||
[mf setNumberFormatter: _numberFormatter];
|
||||
|
||||
return [mf stringFromMeasurement: m];
|
||||
}
|
||||
|
||||
- (NSString *) stringFromJoules: (double)numberInJoules
|
||||
{
|
||||
return [self stringFromValue: numberInJoules unit: NSEnergyFormatterUnitJoule];
|
||||
}
|
||||
|
||||
- (NSString *) unitStringFromValue: (double)value unit: (NSEnergyFormatterUnit)unit
|
||||
{
|
||||
return [self stringFromValue: value unit: unit];
|
||||
}
|
||||
|
||||
- (NSString *) unitStringFromJoules: (double)numberInJoules usedUnit: (NSEnergyFormatterUnit *)unit
|
||||
{
|
||||
*unit = NSEnergyFormatterUnitJoule;
|
||||
return [self stringFromValue: numberInJoules unit: *unit];
|
||||
}
|
||||
|
||||
- (BOOL)getObjectValue: (id *)obj forString: (NSString *)string errorDescription: (NSString **)error
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
|
148
Source/NSLengthFormatter.m
Normal file
148
Source/NSLengthFormatter.m
Normal file
|
@ -0,0 +1,148 @@
|
|||
|
||||
/* Implementation of class NSLengthFormatter
|
||||
Copyright (C) 2019 Free Software Foundation, Inc.
|
||||
|
||||
By: heron
|
||||
Date: Tue Oct 8 13:30:33 EDT 2019
|
||||
|
||||
This file is part of the GNUstep Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
*/
|
||||
|
||||
#include <Foundation/NSLengthFormatter.h>
|
||||
#include <Foundation/NSUnit.h>
|
||||
#include <Foundation/NSMeasurement.h>
|
||||
#include <Foundation/NSMeasurementFormatter.h>
|
||||
|
||||
@implementation NSLengthFormatter
|
||||
|
||||
- (instancetype) init
|
||||
{
|
||||
self = [super init];
|
||||
if(self != nil)
|
||||
{
|
||||
_numberFormatter = nil;
|
||||
_unitStyle = NSFormattingUnitStyleMedium;
|
||||
_isForPersonHeightUse = NO;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
RELEASE(_numberFormatter);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (NSNumberFormatter *) numberFormatter
|
||||
{
|
||||
return _numberFormatter;
|
||||
}
|
||||
|
||||
- (void) setNumberFormatter: (NSNumberFormatter *)formatter
|
||||
{
|
||||
ASSIGN(_numberFormatter, formatter);
|
||||
}
|
||||
|
||||
- (NSFormattingUnitStyle) unitStyle
|
||||
{
|
||||
return _unitStyle;
|
||||
}
|
||||
|
||||
- (void) setUnitStyle: (NSFormattingUnitStyle)style
|
||||
{
|
||||
_unitStyle = style;
|
||||
}
|
||||
|
||||
- (BOOL) isForPersonHeightUse
|
||||
{
|
||||
return _isForPersonHeightUse;
|
||||
}
|
||||
|
||||
- (void) setForPersonHeightUse: (BOOL)flag
|
||||
{
|
||||
_isForPersonHeightUse = flag;
|
||||
}
|
||||
|
||||
- (NSString *) stringFromValue: (double)value unit: (NSLengthFormatterUnit)unit
|
||||
{
|
||||
NSUnit *u = nil;
|
||||
NSMeasurement *m = nil;
|
||||
NSMeasurementFormatter *mf = nil;
|
||||
|
||||
switch(unit)
|
||||
{
|
||||
case NSLengthFormatterUnitMillimeter:
|
||||
u = [NSUnitLength millimeters];
|
||||
break;
|
||||
case NSLengthFormatterUnitCentimeter:
|
||||
u = [NSUnitLength centimeters];
|
||||
break;
|
||||
case NSLengthFormatterUnitMeter:
|
||||
u = [NSUnitLength meters];
|
||||
break;
|
||||
case NSLengthFormatterUnitKilometer:
|
||||
u = [NSUnitLength kilometers];
|
||||
break;
|
||||
case NSLengthFormatterUnitInch:
|
||||
u = [NSUnitLength inches];
|
||||
break;
|
||||
case NSLengthFormatterUnitFoot:
|
||||
u = [NSUnitLength feet];
|
||||
break;
|
||||
case NSLengthFormatterUnitYard:
|
||||
u = [NSUnitLength yards];
|
||||
break;
|
||||
case NSLengthFormatterUnitMile:
|
||||
u = [NSUnitLength miles];
|
||||
break;
|
||||
}
|
||||
|
||||
m = [[NSMeasurement alloc] initWithDoubleValue: value
|
||||
unit: u];
|
||||
AUTORELEASE(m);
|
||||
mf = [[NSMeasurementFormatter alloc] init];
|
||||
AUTORELEASE(mf);
|
||||
[mf setUnitStyle: _unitStyle];
|
||||
[mf setNumberFormatter: _numberFormatter];
|
||||
|
||||
return [mf stringFromMeasurement: m];
|
||||
}
|
||||
|
||||
- (NSString *) stringFromMeters: (double)numberInMeters
|
||||
{
|
||||
return [self stringFromValue: numberInMeters unit: NSLengthFormatterUnitMeter];
|
||||
}
|
||||
|
||||
- (NSString *) unitStringFromValue: (double)value unit: (NSLengthFormatterUnit)unit
|
||||
{
|
||||
return [self stringFromValue: value unit: unit];
|
||||
}
|
||||
|
||||
- (NSString *) unitStringFromMeters: (double)numberInMeters usedUnit: (NSLengthFormatterUnit *)unit
|
||||
{
|
||||
*unit = NSLengthFormatterUnitMeter;
|
||||
return [self stringFromValue: numberInMeters unit: *unit];
|
||||
}
|
||||
|
||||
- (BOOL)getObjectValue: (id *)obj forString: (NSString *)string errorDescription: (NSString **)error
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
|
133
Source/NSMassFormatter.m
Normal file
133
Source/NSMassFormatter.m
Normal file
|
@ -0,0 +1,133 @@
|
|||
|
||||
/* Implementation of class NSMassFormatter
|
||||
Copyright (C) 2019 Free Software Foundation, Inc.
|
||||
|
||||
By: heron
|
||||
Date: Mon Sep 30 15:58:21 EDT 2019
|
||||
|
||||
This file is part of the GNUstep Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
*/
|
||||
|
||||
#include <Foundation/NSMassFormatter.h>
|
||||
#include <Foundation/NSMeasurement.h>
|
||||
#include <Foundation/NSMeasurementFormatter.h>
|
||||
#include <Foundation/NSUnit.h>
|
||||
|
||||
@implementation NSMassFormatter
|
||||
|
||||
- (instancetype) init
|
||||
{
|
||||
self = [super init];
|
||||
if(self != nil)
|
||||
{
|
||||
_numberFormatter = nil;
|
||||
_unitStyle = NSFormattingUnitStyleMedium;
|
||||
_isForPersonMassUse = NO;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSNumberFormatter *) numberFormatter
|
||||
{
|
||||
return _numberFormatter;
|
||||
}
|
||||
|
||||
- (void) setNumberFormatter: (NSNumberFormatter *)formatter
|
||||
{
|
||||
ASSIGN(_numberFormatter, formatter);
|
||||
}
|
||||
|
||||
- (NSFormattingUnitStyle) unitStyle
|
||||
{
|
||||
return _unitStyle;
|
||||
}
|
||||
|
||||
- (void) setUnitStyle: (NSFormattingUnitStyle)style;
|
||||
{
|
||||
_unitStyle = style;
|
||||
}
|
||||
|
||||
- (BOOL) isForPersonMassUse;
|
||||
{
|
||||
return _isForPersonMassUse;
|
||||
}
|
||||
|
||||
- (void) setForPersonMassUse: (BOOL)flag;
|
||||
{
|
||||
_isForPersonMassUse = flag;
|
||||
}
|
||||
|
||||
- (NSString *)stringFromValue: (double)value unit: (NSMassFormatterUnit)unit;
|
||||
{
|
||||
NSUnit *u = nil;
|
||||
NSMeasurement *m = nil;
|
||||
NSMeasurementFormatter *mf = nil;
|
||||
|
||||
switch(unit)
|
||||
{
|
||||
case NSMassFormatterUnitGram:
|
||||
u = [NSUnitMass grams];
|
||||
break;
|
||||
case NSMassFormatterUnitKilogram:
|
||||
u = [NSUnitMass kilograms];
|
||||
break;
|
||||
case NSMassFormatterUnitOunce:
|
||||
u = [NSUnitMass ounces];
|
||||
break;
|
||||
case NSMassFormatterUnitPound:
|
||||
u = [NSUnitMass pounds];
|
||||
break;
|
||||
case NSMassFormatterUnitStone:
|
||||
u = [NSUnitMass stones];
|
||||
break;
|
||||
}
|
||||
|
||||
m = [[NSMeasurement alloc] initWithDoubleValue: value
|
||||
unit: u];
|
||||
AUTORELEASE(m);
|
||||
mf = [[NSMeasurementFormatter alloc] init];
|
||||
AUTORELEASE(mf);
|
||||
[mf setUnitStyle: _unitStyle];
|
||||
[mf setNumberFormatter: _numberFormatter];
|
||||
|
||||
return [mf stringFromMeasurement: m];
|
||||
}
|
||||
|
||||
- (NSString *)stringFromKilograms: (double)numberInKilograms;
|
||||
{
|
||||
return [self stringFromValue: numberInKilograms unit: NSMassFormatterUnitKilogram];
|
||||
}
|
||||
|
||||
- (NSString *)unitStringFromValue: (double)value unit: (NSMassFormatterUnit)unit;
|
||||
{
|
||||
return [self stringFromValue: value unit: unit];
|
||||
}
|
||||
|
||||
- (NSString *)unitStringFromKilograms: (double)numberInKilograms usedUnit: (NSMassFormatterUnit *)unit
|
||||
{
|
||||
*unit = NSMassFormatterUnitKilogram;
|
||||
return [self stringFromValue: numberInKilograms unit: *unit];
|
||||
}
|
||||
|
||||
- (BOOL)getObjectValue: (id*)obj forString: (NSString *)string errorDescription: (NSString **)error
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
|
157
Source/NSMeasurement.m
Normal file
157
Source/NSMeasurement.m
Normal file
|
@ -0,0 +1,157 @@
|
|||
/* Implementation of class NSMeasurement
|
||||
Copyright (C) 2019 Free Software Foundation, Inc.
|
||||
|
||||
By: Gregory John Casamento <greg.casamento@gmail.com>
|
||||
Date: Mon Sep 30 15:58:21 EDT 2019
|
||||
|
||||
This file is part of the GNUstep Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
*/
|
||||
|
||||
#include <Foundation/NSMeasurement.h>
|
||||
#include <Foundation/NSUnit.h>
|
||||
#include <Foundation/NSException.h>
|
||||
#include <Foundation/NSArchiver.h>
|
||||
#include <Foundation/NSKeyedArchiver.h>
|
||||
|
||||
@implementation NSMeasurement
|
||||
// Creating Measurements
|
||||
- (instancetype)initWithDoubleValue: (double)doubleValue
|
||||
unit: (NSUnit *)unit
|
||||
{
|
||||
self = [super init];
|
||||
if(self != nil)
|
||||
{
|
||||
ASSIGNCOPY(_unit, unit);
|
||||
_doubleValue = doubleValue;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
RELEASE(_unit);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
// Accessing unit and value
|
||||
- (NSUnit *) unit
|
||||
{
|
||||
return _unit;
|
||||
}
|
||||
|
||||
- (double) doubleValue
|
||||
{
|
||||
return _doubleValue;
|
||||
}
|
||||
|
||||
// Conversion
|
||||
- (BOOL) canBeConvertedToUnit: (NSUnit *)unit
|
||||
{
|
||||
return ([unit isKindOfClass: [_unit class]] &&
|
||||
[unit respondsToSelector: @selector(converter)]);
|
||||
}
|
||||
|
||||
- (NSMeasurement *) measurementByConvertingToUnit: (NSUnit *)unit
|
||||
{
|
||||
NSMeasurement *result = nil;
|
||||
double val = 0.0;
|
||||
if([self canBeConvertedToUnit: unit])
|
||||
{
|
||||
// Do conversion...
|
||||
NSUnitConverter *c = [(NSDimension *)unit converter];
|
||||
val = [c baseUnitValueFromValue: _doubleValue];
|
||||
result = [[NSMeasurement alloc] initWithDoubleValue: val unit: unit];
|
||||
AUTORELEASE(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"Cannot convert from %@ to %@", _unit, unit];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Operating
|
||||
- (NSMeasurement *) measurementByAddingMeasurement: (NSMeasurement *)measurement
|
||||
{
|
||||
NSMeasurement *newMeasurement = [measurement measurementByConvertingToUnit: _unit];
|
||||
NSMeasurement *result = nil;
|
||||
double v = 0.0;
|
||||
|
||||
v = _doubleValue + [newMeasurement doubleValue];
|
||||
result = [[NSMeasurement alloc] initWithDoubleValue: v unit: _unit];
|
||||
AUTORELEASE(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
- (NSMeasurement *) measurementBySubtractingMeasurement: (NSMeasurement *)measurement
|
||||
{
|
||||
NSMeasurement *newMeasurement = [measurement measurementByConvertingToUnit: _unit];
|
||||
NSMeasurement *result = nil;
|
||||
double v = 0.0;
|
||||
|
||||
v = _doubleValue - [newMeasurement doubleValue];
|
||||
result = [[NSMeasurement alloc] initWithDoubleValue: v unit: _unit];
|
||||
AUTORELEASE(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// NSCopying
|
||||
- (id) copyWithZone: (NSZone *)zone
|
||||
{
|
||||
return [[[self class] allocWithZone: zone] initWithDoubleValue: _doubleValue
|
||||
unit: _unit];
|
||||
}
|
||||
|
||||
// NSCoding
|
||||
- (void) encodeWithCoder: (NSCoder *)coder
|
||||
{
|
||||
if([coder allowsKeyedCoding])
|
||||
{
|
||||
// TODO: Verify that this is the correct encoding...
|
||||
[coder encodeObject: _unit forKey: @"unit"];
|
||||
[coder encodeDouble: _doubleValue forKey: @"doubleValue"];
|
||||
}
|
||||
else
|
||||
{
|
||||
[coder encodeObject: _unit];
|
||||
[coder encodeValueOfObjCType: @encode(double) at: &_doubleValue];
|
||||
}
|
||||
}
|
||||
|
||||
- (id) initWithCoder: (NSCoder *)coder
|
||||
{
|
||||
if((self = [super init]) != nil)
|
||||
{
|
||||
if([coder allowsKeyedCoding])
|
||||
{
|
||||
_unit = [coder decodeObjectForKey: @"unit"];
|
||||
_doubleValue = [coder decodeDoubleForKey: @"doubleValue"];
|
||||
}
|
||||
else
|
||||
{
|
||||
_unit = [coder decodeObject];
|
||||
[coder decodeValueOfObjCType: @encode(double) at: &_doubleValue];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@end
|
||||
|
130
Source/NSMeasurementFormatter.m
Normal file
130
Source/NSMeasurementFormatter.m
Normal file
|
@ -0,0 +1,130 @@
|
|||
/* Implementation of class NSMeasurementFormatter
|
||||
Copyright (C) 2019 Free Software Foundation, Inc.
|
||||
|
||||
By: Gregory John Casamento <greg.casamento@gmail.com>
|
||||
Date: Mon Sep 30 15:58:21 EDT 2019
|
||||
|
||||
This file is part of the GNUstep Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
*/
|
||||
|
||||
#include <Foundation/NSMeasurementFormatter.h>
|
||||
#include <Foundation/NSLocale.h>
|
||||
#include <Foundation/NSMeasurement.h>
|
||||
#include <Foundation/NSNumberFormatter.h>
|
||||
#include <Foundation/NSUnit.h>
|
||||
|
||||
@implementation NSMeasurementFormatter
|
||||
|
||||
- (instancetype) init
|
||||
{
|
||||
self = [super init];
|
||||
if(self != nil)
|
||||
{
|
||||
_unitOptions = NSMeasurementFormatterUnitOptionsProvidedUnit;
|
||||
_unitStyle = NSFormattingUnitStyleMedium;
|
||||
_locale = RETAIN([NSLocale currentLocale]);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
RELEASE(_locale);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (NSMeasurementFormatterUnitOptions) unitOptions
|
||||
{
|
||||
return _unitOptions;
|
||||
}
|
||||
|
||||
- (void) setUnitOptions: (NSMeasurementFormatterUnitOptions) unitOptions
|
||||
{
|
||||
_unitOptions = unitOptions;
|
||||
}
|
||||
|
||||
- (NSFormattingUnitStyle) unitStyle
|
||||
{
|
||||
return _unitStyle;
|
||||
}
|
||||
|
||||
- (void) setUnitStyle: (NSFormattingUnitStyle)style
|
||||
{
|
||||
_unitStyle = style;
|
||||
}
|
||||
|
||||
- (NSLocale *) locale
|
||||
{
|
||||
return _locale;
|
||||
}
|
||||
|
||||
- (void) setLocale: (NSLocale *)locale
|
||||
{
|
||||
ASSIGNCOPY(_locale, locale);
|
||||
}
|
||||
|
||||
- (NSNumberFormatter *) numberFormatter
|
||||
{
|
||||
return _numberFormatter;
|
||||
}
|
||||
|
||||
- (void) setNumberFormatter: (NSNumberFormatter *)numberFormatter
|
||||
{
|
||||
ASSIGNCOPY(_numberFormatter, numberFormatter);
|
||||
}
|
||||
|
||||
- (NSString *)stringFromMeasurement: (NSMeasurement *)measurement
|
||||
{
|
||||
NSString *result = nil;
|
||||
NSNumber *num = [NSNumber numberWithDouble: [measurement doubleValue]];
|
||||
NSUnit *u = [measurement unit];
|
||||
|
||||
result = [_numberFormatter stringForObjectValue: num];
|
||||
switch(_unitStyle)
|
||||
{
|
||||
case NSFormattingUnitStyleShort:
|
||||
case NSFormattingUnitStyleMedium:
|
||||
case NSFormattingUnitStyleLong:
|
||||
result = [result stringByAppendingString: [self stringFromUnit: u]];
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
- (NSString *)stringFromUnit: (NSUnit *)unit
|
||||
{
|
||||
return [unit symbol];
|
||||
}
|
||||
|
||||
- (NSString *)stringForObjectValue: (id)obj
|
||||
{
|
||||
NSString *result = nil;
|
||||
if([obj isKindOfClass: [NSMeasurement class]])
|
||||
{
|
||||
result = [self stringFromMeasurement: obj];
|
||||
}
|
||||
else if([obj isKindOfClass: [NSUnit class]])
|
||||
{
|
||||
result = [self stringFromUnit: obj];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@end
|
||||
|
1771
Source/NSUnit.m
Normal file
1771
Source/NSUnit.m
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue